Index: src/molecule_graph.cpp
===================================================================
--- src/molecule_graph.cpp	(revision 920c708861f3c5528d44b9973722fa719f892ce0)
+++ src/molecule_graph.cpp	(revision 1024cbc27eef62ed70b770868d078bd3df38ada6)
@@ -20,4 +20,6 @@
 #include "World.hpp"
 #include "Helpers/fast_functions.hpp"
+#include "Helpers/Assert.hpp"
+
 
 struct BFSAccounting
@@ -80,5 +82,7 @@
       flip(atom1, atom2);
     Walker = FindAtom(atom1);
+    ASSERT(Walker,"Could not find an atom with the ID given in dbond file");
     OtherWalker = FindAtom(atom2);
+    ASSERT(OtherWalker,"Could not find an atom with the ID given in dbond file");
     AddBond(Walker, OtherWalker); //Add the bond between the two atoms with respective indices.
   }
@@ -109,5 +113,4 @@
   atom *Walker = NULL;
   atom *OtherWalker = NULL;
-  atom **AtomMap = NULL;
   int n[NDIM];
   double MinDistance, MaxDistance;
@@ -134,8 +137,7 @@
 
   // count atoms in molecule = dimension of matrix (also give each unique name and continuous numbering)
-  CountAtoms();
-  DoLog(1) && (Log() << Verbose(1) << "AtomCount " << AtomCount << " and bonddistance is " << bonddistance << "." << endl);
-
-  if ((AtomCount > 1) && (bonddistance > 1.)) {
+  DoLog(1) && (Log() << Verbose(1) << "AtomCount " << getAtomCount() << " and bonddistance is " << bonddistance << "." << endl);
+
+  if ((getAtomCount() > 1) && (bonddistance > 1.)) {
     DoLog(2) && (Log() << Verbose(2) << "Creating Linked Cell structure ... " << endl);
     LC = new LinkedCell(this, bonddistance);
@@ -143,11 +145,9 @@
     // create a list to map Tesselpoint::nr to atom *
     DoLog(2) && (Log() << Verbose(2) << "Creating TesselPoint to atom map ... " << endl);
-    AtomMap = new atom *[AtomCount];
-    for (int i=0;i<AtomCount;i++)
-      AtomMap[i] = NULL;
-    Walker = start;
-    while (Walker->next != end) {
-      Walker = Walker->next;
-      AtomMap[Walker->nr] = Walker;
+
+    // set numbers for atoms that can later be used
+    int i=0;
+    for(internal_iterator iter = atoms.begin();iter!= atoms.end(); ++iter){
+      (*iter)->nr = i++;
     }
 
@@ -161,6 +161,7 @@
           if (List != NULL) {
             for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
-              Walker = AtomMap[(*Runner)->nr];
-//              Log() << Verbose(0) << "Current Atom is " << *Walker << "." << endl;
+              Walker = dynamic_cast<atom*>(*Runner);
+              ASSERT(Walker,"Tesselpoint that was not an atom retrieved from LinkedNode");
+              //Log() << Verbose(0) << "Current Atom is " << *Walker << "." << endl;
               // 3c. check for possible bond between each atom in this and every one in the 27 cells
               for (n[0] = -1; n[0] <= 1; n[0]++)
@@ -172,9 +173,9 @@
                       for (LinkedCell::LinkedNodes::const_iterator OtherRunner = OtherList->begin(); OtherRunner != OtherList->end(); OtherRunner++) {
                         if ((*OtherRunner)->nr > Walker->nr) {
-                          OtherWalker = AtomMap[(*OtherRunner)->nr];
-//                          Log() << Verbose(0) << "Current other Atom is " << *OtherWalker << "." << endl;
-                          const double distance = OtherWalker->x.PeriodicDistanceSquared(Walker->x, cell_size);
-//                          Log() << Verbose(1) << "Checking distance " << distance << " against typical bond length of " << bonddistance*bonddistance << "." << endl;
+                          OtherWalker = dynamic_cast<atom*>(*OtherRunner);
+                          ASSERT(OtherWalker,"TesselPoint that was not an atom retrieved from LinkedNode");
+                          //Log() << Verbose(1) << "Checking distance " << OtherWalker->x.PeriodicDistanceSquared(&(Walker->x), cell_size) << " against typical bond length of " << bonddistance*bonddistance << "." << endl;
                           (BG->*minmaxdistance)(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem);
+                          const double distance = OtherWalker->x.PeriodicDistanceSquared(Walker->x,cell_size);
                           const bool status = (distance <= MaxDistance * MaxDistance) && (distance >= MinDistance * MinDistance);
 //                          Log() << Verbose(1) << "MinDistance is " << MinDistance << " and MaxDistance is " << MaxDistance << "." << endl;
@@ -196,5 +197,4 @@
           }
         }
-    delete[](AtomMap);
     delete (LC);
     DoLog(1) && (Log() << Verbose(1) << "I detected " << BondCount << " bonds in the molecule with distance " << BondDistance << "." << endl);
@@ -207,5 +207,5 @@
     ActOnAllAtoms( &atom::OutputBondOfAtom );
   } else
-    DoLog(1) && (Log() << Verbose(1) << "AtomCount is " << AtomCount << ", thus no bonds, no connections!." << endl);
+    DoLog(1) && (Log() << Verbose(1) << "AtomCount is " << getAtomCount() << ", thus no bonds, no connections!." << endl);
   DoLog(0) && (Log() << Verbose(0) << "End of CreateAdjacencyList." << endl);
   if (free_BG)
@@ -249,5 +249,5 @@
     DoLog(0) && (Log() << Verbose(0) << " done." << endl);
   } else {
-    DoLog(1) && (Log() << Verbose(1) << "BondCount is " << BondCount << ", no bonds between any of the " << AtomCount << " atoms." << endl);
+    DoLog(1) && (Log() << Verbose(1) << "BondCount is " << BondCount << ", no bonds between any of the " << getAtomCount() << " atoms." << endl);
   }
   DoLog(0) && (Log() << Verbose(0) << No << " bonds could not be corrected." << endl);
@@ -469,5 +469,5 @@
 void DepthFirstSearchAnalysis_Init(struct DFSAccounting &DFS, const molecule * const mol)
 {
-  DFS.AtomStack = new StackClass<atom *> (mol->AtomCount);
+  DFS.AtomStack = new StackClass<atom *> (mol->getAtomCount());
   DFS.CurrentGraphNr = 0;
   DFS.ComponentNumber = 0;
@@ -510,11 +510,11 @@
   bond *Binder = NULL;
 
-  if (AtomCount == 0)
+  if (getAtomCount() == 0)
     return SubGraphs;
   DoLog(0) && (Log() << Verbose(0) << "Begin of DepthFirstSearchAnalysis" << endl);
   DepthFirstSearchAnalysis_Init(DFS, this);
 
-  DFS.Root = start->next;
-  while (DFS.Root != end) { // if there any atoms at all
+  for (molecule::const_iterator iter = begin(); iter != end();) {
+    DFS.Root = *iter;
     // (1) mark all edges unused, empty stack, set atom->GraphNr = -1 for all
     DFS.AtomStack->ClearStack();
@@ -556,8 +556,8 @@
 
     // step on to next root
-    while ((DFS.Root != end) && (DFS.Root->GraphNr != -1)) {
-      //Log() << Verbose(1) << "Current next subgraph root candidate is " << Root->Name << "." << endl;
-      if (DFS.Root->GraphNr != -1) // if already discovered, step on
-        DFS.Root = DFS.Root->next;
+    while ((iter != end()) && ((*iter)->GraphNr != -1)) {
+      //Log() << Verbose(1) << "Current next subgraph root candidate is " << (*iter)->Name << "." << endl;
+      if ((*iter)->GraphNr != -1) // if already discovered, step on
+        iter++;
     }
   }
@@ -864,13 +864,12 @@
   if (MinRingSize != -1) { // if rings are present
     // go over all atoms
-    Root = mol->start;
-    while (Root->next != mol->end) {
-      Root = Root->next;
-
-      if (MinimumRingSize[Root->GetTrueFather()->nr] == mol->AtomCount) { // check whether MinimumRingSize is set, if not BFS to next where it is
+    for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
+      Root = *iter;
+
+      if (MinimumRingSize[Root->GetTrueFather()->nr] == mol->getAtomCount()) { // check whether MinimumRingSize is set, if not BFS to next where it is
         Walker = Root;
 
         //Log() << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl;
-        CyclicStructureAnalysis_BFSToNextCycle(Root, Walker, MinimumRingSize, mol->AtomCount);
+        CyclicStructureAnalysis_BFSToNextCycle(Root, Walker, MinimumRingSize, mol->getAtomCount());
 
       }
@@ -902,5 +901,5 @@
   int MinRingSize = -1;
 
-  InitializeBFSAccounting(BFS, AtomCount);
+  InitializeBFSAccounting(BFS, getAtomCount());
 
   //Log() << Verbose(1) << "Back edge list - ";
@@ -1145,5 +1144,5 @@
     CurrentBondsOfAtom = -1; // we count one too far due to line end
     // parse into structure
-    if ((AtomNr >= 0) && (AtomNr < AtomCount)) {
+    if ((AtomNr >= 0) && (AtomNr < getAtomCount())) {
       Walker = ListOfAtoms[AtomNr];
       while (!line.eof())
@@ -1324,5 +1323,5 @@
     AddedAtomList[Root->nr] = Mol->AddCopyAtom(Root);
 
-  BreadthFirstSearchAdd_Init(BFS, Root, BondOrder, AtomCount, AddedAtomList);
+  BreadthFirstSearchAdd_Init(BFS, Root, BondOrder, getAtomCount(), AddedAtomList);
 
   // and go on ... Queue always contains all lightgray vertices
@@ -1382,14 +1381,10 @@
   // fill parent list with sons
   DoLog(3) && (Log() << Verbose(3) << "Filling Parent List." << endl);
-  atom *Walker = mol->start;
-  while (Walker->next != mol->end) {
-    Walker = Walker->next;
-    ParentList[Walker->father->nr] = Walker;
+  for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
+    ParentList[(*iter)->father->nr] = (*iter);
     // Outputting List for debugging
-    DoLog(4) && (Log() << Verbose(4) << "Son[" << Walker->father->nr << "] of " << Walker->father << " is " << ParentList[Walker->father->nr] << "." << endl);
-  }
-
-}
-;
+    DoLog(4) && (Log() << Verbose(4) << "Son[" << (*iter)->father->nr << "] of " << (*iter)->father << " is " << ParentList[(*iter)->father->nr] << "." << endl);
+  }
+};
 
 void BuildInducedSubgraph_Finalize(atom **&ParentList)
@@ -1402,20 +1397,17 @@
 {
   bool status = true;
-  atom *Walker = NULL;
   atom *OtherAtom = NULL;
   // check each entry of parent list and if ok (one-to-and-onto matching) create bonds
   DoLog(3) && (Log() << Verbose(3) << "Creating bonds." << endl);
-  Walker = Father->start;
-  while (Walker->next != Father->end) {
-    Walker = Walker->next;
-    if (ParentList[Walker->nr] != NULL) {
-      if (ParentList[Walker->nr]->father != Walker) {
+  for (molecule::const_iterator iter = Father->begin(); iter != Father->end(); ++iter) {
+    if (ParentList[(*iter)->nr] != NULL) {
+      if (ParentList[(*iter)->nr]->father != (*iter)) {
         status = false;
       } else {
-        for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
-          OtherAtom = (*Runner)->GetOtherAtom(Walker);
+        for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
+          OtherAtom = (*Runner)->GetOtherAtom((*iter));
           if (ParentList[OtherAtom->nr] != NULL) { // if otheratom is also a father of an atom on this molecule, create the bond
-            DoLog(4) && (Log() << Verbose(4) << "Endpoints of Bond " << (*Runner) << " are both present: " << ParentList[Walker->nr]->getName() << " and " << ParentList[OtherAtom->nr]->getName() << "." << endl);
-            mol->AddBond(ParentList[Walker->nr], ParentList[OtherAtom->nr], (*Runner)->BondDegree);
+            DoLog(4) && (Log() << Verbose(4) << "Endpoints of Bond " << (*Runner) << " are both present: " << ParentList[(*iter)->nr]->getName() << " and " << ParentList[OtherAtom->nr]->getName() << "." << endl);
+            mol->AddBond(ParentList[(*iter)->nr], ParentList[OtherAtom->nr], (*Runner)->BondDegree);
           }
         }
@@ -1440,7 +1432,6 @@
   bool status = true;
   atom **ParentList = NULL;
-
   DoLog(2) && (Log() << Verbose(2) << "Begin of BuildInducedSubgraph." << endl);
-  BuildInducedSubgraph_Init(ParentList, Father->AtomCount);
+  BuildInducedSubgraph_Init(ParentList, Father->getAtomCount());
   BuildInducedSubgraph_FillParentList(this, Father, ParentList);
   status = BuildInducedSubgraph_CreateBondsFromParent(this, Father, ParentList);
