Index: src/molecules.cpp
===================================================================
--- src/molecules.cpp	(revision 21c0171a2ffa8aa89b2345abf037899c7dbedf2b)
+++ src/molecules.cpp	(revision 5466f3c25c7264956c286b80f3090bf2e9b5b3d9)
@@ -1469,30 +1469,25 @@
 bool molecule::Output(ofstream *out)
 {
-	element *runner;
 	atom *walker = NULL;
-	int ElementNo, AtomNo;
+	int ElementNo[MAX_ELEMENTS], AtomNo[MAX_ELEMENTS];
+	int CurrentMaxElement = 0;
 	CountElements();
 
+	for (int i=0;i<MAX_ELEMENTS;++i) {
+	  AtomNo[i] = 0;
+	  ElementNo[i] = 0;
+	}
 	if (out == NULL) {
 		return false;
 	} else {
 		*out << "#Ion_TypeNr._Nr.R[0]		R[1]		R[2]		MoveType (0 MoveIon, 1 FixedIon)" << endl;
-		ElementNo = 0;
-		runner = elemente->start;
-		while (runner->next != elemente->end) { // go through every element
-			runner = runner->next;
-			if (ElementsInMolecule[runner->Z]) { // if this element got atoms
-				ElementNo++;
-				AtomNo = 0;
-				walker = start;
-				while (walker->next != end) { // go through every atom of this element
-					walker = walker->next;
-					if (walker->type == runner) { // if this atom fits to element
-						AtomNo++;
-						walker->Output(ElementNo, AtomNo, out); // removed due to trajectories
-					}
-				}
-			}
-		}
+    walker = start;
+    while (walker->next != end) { // go through every atom of this element
+      walker = walker->next;
+      if (ElementNo[walker->type->Z] == 0) // new element
+        ElementNo[walker->type->Z] = CurrentMaxElement++;
+      AtomNo[walker->type->Z]++;
+      walker->Output(ElementNo[walker->type->Z], AtomNo[walker->type->Z], out); // removed due to trajectories
+    }
 		return true;
 	}
@@ -1504,7 +1499,7 @@
 bool molecule::OutputTrajectories(ofstream *out)
 {
-	element *runner = NULL;
 	atom *walker = NULL;
-	int ElementNo, AtomNo;
+  int ElementNo[MAX_ELEMENTS], AtomNo[MAX_ELEMENTS];
+  int CurrentMaxElement = 0;
 	CountElements();
 
@@ -1518,28 +1513,23 @@
 				*out << "# ====== MD step " << step << " =========" << endl;
 			}
-			ElementNo = 0;
-			runner = elemente->start;
-			while (runner->next != elemente->end) { // go through every element
-				runner = runner->next;
-				if (ElementsInMolecule[runner->Z]) { // if this element got atoms
-					ElementNo++;
-					AtomNo = 0;
-					walker = start;
-					while (walker->next != end) { // go through every atom of this element
-						walker = walker->next;
-						if (walker->type == runner) { // if this atom fits to element
-							AtomNo++;
-							*out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t"	<< fixed << setprecision(9) << showpoint;
-							*out << Trajectories[walker].R.at(step).x[0] << "\t" << Trajectories[walker].R.at(step).x[1] << "\t" << Trajectories[walker].R.at(step).x[2];
-							*out << "\t" << walker->FixedIon;
-							if (Trajectories[walker].U.at(step).Norm() > MYEPSILON)
-								*out << "\t" << scientific << setprecision(6) << Trajectories[walker].U.at(step).x[0] << "\t" << Trajectories[walker].U.at(step).x[1] << "\t" << Trajectories[walker].U.at(step).x[2] << "\t";
-							if (Trajectories[walker].F.at(step).Norm() > MYEPSILON)
-								*out << "\t" << scientific << setprecision(6) << Trajectories[walker].F.at(step).x[0] << "\t" << Trajectories[walker].F.at(step).x[1] << "\t" << Trajectories[walker].F.at(step).x[2] << "\t";
-							*out << "\t# Number in molecule " << walker->nr << endl;
-						}
-					}
-				}
-			}
+		  for (int i=0;i<MAX_ELEMENTS;++i) {
+		    AtomNo[i] = 0;
+		    ElementNo[i] = 0;
+		  }
+      walker = start;
+      while (walker->next != end) { // go through every atom of this element
+        walker = walker->next;
+        if (ElementNo[walker->type->Z] == 0) // new element
+          ElementNo[walker->type->Z] = CurrentMaxElement++;
+        AtomNo[walker->type->Z]++;
+        *out << "Ion_Type" << ElementNo[walker->type->Z] << "_" << AtomNo[walker->type->Z] << "\t"	<< fixed << setprecision(9) << showpoint;
+        *out << Trajectories[walker].R.at(step).x[0] << "\t" << Trajectories[walker].R.at(step).x[1] << "\t" << Trajectories[walker].R.at(step).x[2];
+        *out << "\t" << walker->FixedIon;
+        if (Trajectories[walker].U.at(step).Norm() > MYEPSILON)
+          *out << "\t" << scientific << setprecision(6) << Trajectories[walker].U.at(step).x[0] << "\t" << Trajectories[walker].U.at(step).x[1] << "\t" << Trajectories[walker].U.at(step).x[2] << "\t";
+        if (Trajectories[walker].F.at(step).Norm() > MYEPSILON)
+          *out << "\t" << scientific << setprecision(6) << Trajectories[walker].F.at(step).x[0] << "\t" << Trajectories[walker].F.at(step).x[1] << "\t" << Trajectories[walker].F.at(step).x[2] << "\t";
+        *out << "\t# Number in molecule " << walker->nr << endl;
+      }
 		}
 		return true;
@@ -1613,7 +1603,6 @@
 {
 	atom *walker = NULL;
-	int AtomNo = 0, ElementNo;
+	int AtomNo = 0;
 	time_t now;
-	element *runner = NULL;
 
 	now = time((time_t *)NULL);	 // Get the system time and put it into 'now' as 'calender time'
@@ -1625,19 +1614,9 @@
 	if (out != NULL) {
 		*out << AtomNo << "\n\tCreated by molecuilder on " << ctime(&now);
-		ElementNo = 0;
-		runner = elemente->start;
-		while (runner->next != elemente->end) { // go through every element
-			runner = runner->next;
-			if (ElementsInMolecule[runner->Z]) { // if this element got atoms
-				ElementNo++;
-				walker = start;
-				while (walker->next != end) { // go through every atom of this element
-					walker = walker->next;
-					if (walker->type == runner) { // if this atom fits to element
-						walker->OutputXYZLine(out);
-					}
-				}
-			}
-		}
+    walker = start;
+    while (walker->next != end) { // go through every atom of this element
+      walker = walker->next;
+      walker->OutputXYZLine(out);
+    }
 		return true;
 	} else
