Index: src/molecule_graph.cpp
===================================================================
--- src/molecule_graph.cpp	(revision 174e0ece471da3cb17f2cbf08ba655f90c27f316)
+++ src/molecule_graph.cpp	(revision ef9aaec99bd2d3f48063f5f75506c3db39493c39)
@@ -494,4 +494,213 @@
 };
 
+/**  initialise each vertex as white with no predecessor, empty queue, color Root lightgray.
+ *
+ */
+void InitializeAccounting(atom **&PredecessorList, int *&ShortestPathList, enum Shading *&ColorList, int AtomCount)
+{
+  for (int i=AtomCount;i--;) {
+    PredecessorList[i] = NULL;
+    ShortestPathList[i] = -1;
+    ColorList[i] = white;
+  }
+};
+
+void ResetAccounting(atom *&Walker, class StackClass<atom *> *&TouchedStack, int *&ShortestPathList, class StackClass<atom *> *&BFSStack)
+{
+  ShortestPathList[Walker->nr] = 0;
+  BFSStack->ClearStack();  // start with empty BFS stack
+  BFSStack->Push(Walker);
+  TouchedStack->Push(Walker);
+};
+
+void CyclicBFSFromRootToRoot(ofstream *out, atom *&Root, bond *&BackEdge, class StackClass<atom *> *&TouchedStack, int *&ShortestPathList, atom **&PredecessorList, class StackClass<atom *> *&BFSStack, enum Shading *&ColorList)
+{
+  atom *Walker = NULL;
+  atom *OtherAtom = NULL;
+  do {  // look for Root
+    Walker = BFSStack->PopFirst();
+    *out << Verbose(2) << "Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "." << endl;
+    for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
+      if ((*Runner) != BackEdge) { // only walk along DFS spanning tree (otherwise we always find SP of one being backedge Binder)
+        OtherAtom = (*Runner)->GetOtherAtom(Walker);
+  #ifdef ADDHYDROGEN
+        if (OtherAtom->type->Z != 1) {
+  #endif
+          *out << Verbose(2) << "Current OtherAtom is: " << OtherAtom->Name << " for bond " << *(*Runner) << "." << endl;
+          if (ColorList[OtherAtom->nr] == white) {
+            TouchedStack->Push(OtherAtom);
+            ColorList[OtherAtom->nr] = lightgray;
+            PredecessorList[OtherAtom->nr] = Walker;  // Walker is the predecessor
+            ShortestPathList[OtherAtom->nr] = ShortestPathList[Walker->nr]+1;
+            *out << Verbose(2) << "Coloring OtherAtom " << OtherAtom->Name << " lightgray, its predecessor is " << Walker->Name << " and its Shortest Path is " << ShortestPathList[OtherAtom->nr] << " egde(s) long." << endl;
+            //if (ShortestPathList[OtherAtom->nr] < MinimumRingSize[Walker->GetTrueFather()->nr]) { // Check for maximum distance
+              *out << Verbose(3) << "Putting OtherAtom into queue." << endl;
+              BFSStack->Push(OtherAtom);
+            //}
+          } else {
+            *out << Verbose(3) << "Not Adding, has already been visited." << endl;
+          }
+          if (OtherAtom == Root)
+            break;
+  #ifdef ADDHYDROGEN
+        } else {
+          *out << Verbose(2) << "Skipping hydrogen atom " << *OtherAtom << "." << endl;
+          ColorList[OtherAtom->nr] = black;
+        }
+  #endif
+      } else {
+        *out << Verbose(2) << "Bond " << *(*Runner) << " not Visiting, is the back edge." << endl;
+      }
+    }
+    ColorList[Walker->nr] = black;
+    *out << Verbose(1) << "Coloring Walker " << Walker->Name << " black." << endl;
+    if (OtherAtom == Root) { // if we have found the root, check whether this cycle wasn't already found beforehand
+      // step through predecessor list
+      while (OtherAtom != BackEdge->rightatom) {
+        if (!OtherAtom->GetTrueFather()->IsCyclic)  // if one bond in the loop is not marked as cyclic, we haven't found this cycle yet
+          break;
+        else
+          OtherAtom = PredecessorList[OtherAtom->nr];
+      }
+      if (OtherAtom == BackEdge->rightatom) { // if each atom in found cycle is cyclic, loop's been found before already
+        *out << Verbose(3) << "This cycle was already found before, skipping and removing seeker from search." << endl;\
+        do {
+          OtherAtom = TouchedStack->PopLast();
+          if (PredecessorList[OtherAtom->nr] == Walker) {
+            *out << Verbose(4) << "Removing " << *OtherAtom << " from lists and stacks." << endl;
+            PredecessorList[OtherAtom->nr] = NULL;
+            ShortestPathList[OtherAtom->nr] = -1;
+            ColorList[OtherAtom->nr] = white;
+            BFSStack->RemoveItem(OtherAtom);
+          }
+        } while ((!TouchedStack->IsEmpty()) && (PredecessorList[OtherAtom->nr] == NULL));
+        TouchedStack->Push(OtherAtom);  // last was wrongly popped
+        OtherAtom = BackEdge->rightatom; // set to not Root
+      } else
+        OtherAtom = Root;
+    }
+  } while ((!BFSStack->IsEmpty()) && (OtherAtom != Root) && (OtherAtom != NULL)); // || (ShortestPathList[OtherAtom->nr] < MinimumRingSize[Walker->GetTrueFather()->nr])));
+};
+
+void RetrieveCycleMembers(ofstream *out, atom *&Root, atom *&OtherAtom, bond *&BackEdge, atom **&PredecessorList, int *&MinimumRingSize, int &MinRingSize)
+{
+  atom *Walker = NULL;
+  int NumCycles = 0;
+  int RingSize = -1;
+
+  if (OtherAtom == Root) {
+    // now climb back the predecessor list and thus find the cycle members
+    NumCycles++;
+    RingSize = 1;
+    Root->GetTrueFather()->IsCyclic = true;
+    *out << Verbose(1) << "Found ring contains: ";
+    Walker = Root;
+    while (Walker != BackEdge->rightatom) {
+      *out << Walker->Name << " <-> ";
+      Walker = PredecessorList[Walker->nr];
+      Walker->GetTrueFather()->IsCyclic = true;
+      RingSize++;
+    }
+    *out << Walker->Name << "  with a length of " << RingSize << "." << endl << endl;
+    // walk through all and set MinimumRingSize
+    Walker = Root;
+    MinimumRingSize[Walker->GetTrueFather()->nr] = RingSize;
+    while (Walker != BackEdge->rightatom) {
+      Walker = PredecessorList[Walker->nr];
+      if (RingSize < MinimumRingSize[Walker->GetTrueFather()->nr])
+        MinimumRingSize[Walker->GetTrueFather()->nr] = RingSize;
+    }
+    if ((RingSize < MinRingSize) || (MinRingSize == -1))
+      MinRingSize = RingSize;
+  } else {
+    *out << Verbose(1) << "No ring containing " << *Root << " with length equal to or smaller than " << MinimumRingSize[Walker->GetTrueFather()->nr] << " found." << endl;
+  }
+};
+
+void CleanAccounting(class StackClass<atom *> *&TouchedStack, atom **&PredecessorList, int *&ShortestPathList, enum Shading *&ColorList)
+{
+  atom *Walker = NULL;
+  while (!TouchedStack->IsEmpty()){
+    Walker = TouchedStack->PopFirst();
+    PredecessorList[Walker->nr] = NULL;
+    ShortestPathList[Walker->nr] = -1;
+    ColorList[Walker->nr] = white;
+  }
+};
+
+
+void BFSToNextCycle(ofstream *out, atom *&Root, atom *&Walker, bond *&BackEdge, int *&MinimumRingSize, int AtomCount)
+{
+  atom **PredecessorList = Malloc<atom*>(AtomCount, "molecule::CyclicStructureAnalysis: **PredecessorList");
+  int *ShortestPathList = Malloc<int>(AtomCount, "molecule::CyclicStructureAnalysis: *ShortestPathList");
+  enum Shading *ColorList = Malloc<enum Shading>(AtomCount, "molecule::CyclicStructureAnalysis: *ColorList");
+  class StackClass<atom *> *BFSStack = new StackClass<atom *> (AtomCount);   // will hold the current ring
+  class StackClass<atom *> *TouchedStack = new StackClass<atom *> (AtomCount);   // contains all "touched" atoms (that need to be reset after BFS loop)
+  atom *OtherAtom = Walker;
+
+  InitializeAccounting(PredecessorList, ShortestPathList, ColorList, AtomCount);
+
+  ResetAccounting(Walker, TouchedStack, ShortestPathList, BFSStack);
+  while (OtherAtom != NULL) {  // look for Root
+    Walker = BFSStack->PopFirst();
+    //*out << Verbose(2) << "Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "." << endl;
+    for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
+      if (((*Runner) != BackEdge) || (Walker->ListOfBonds.size() == 1)) { // only walk along DFS spanning tree (otherwise we always find SP of 1 being backedge Binder), but terminal hydrogens may be connected via backedge, hence extra check
+        OtherAtom = (*Runner)->GetOtherAtom(Walker);
+        //*out << Verbose(2) << "Current OtherAtom is: " << OtherAtom->Name << " for bond " << *Binder << "." << endl;
+        if (ColorList[OtherAtom->nr] == white) {
+          TouchedStack->Push(OtherAtom);
+          ColorList[OtherAtom->nr] = lightgray;
+          PredecessorList[OtherAtom->nr] = Walker;  // Walker is the predecessor
+          ShortestPathList[OtherAtom->nr] = ShortestPathList[Walker->nr]+1;
+          //*out << Verbose(2) << "Coloring OtherAtom " << OtherAtom->Name << " lightgray, its predecessor is " << Walker->Name << " and its Shortest Path is " << ShortestPathList[OtherAtom->nr] << " egde(s) long." << endl;
+          if (OtherAtom->GetTrueFather()->IsCyclic) { // if the other atom is connected to a ring
+            MinimumRingSize[Root->GetTrueFather()->nr] = ShortestPathList[OtherAtom->nr]+MinimumRingSize[OtherAtom->GetTrueFather()->nr];
+            OtherAtom = NULL; //break;
+            break;
+          } else
+            BFSStack->Push(OtherAtom);
+        } else {
+          //*out << Verbose(3) << "Not Adding, has already been visited." << endl;
+        }
+      } else {
+        //*out << Verbose(3) << "Not Visiting, is a back edge." << endl;
+      }
+    }
+    ColorList[Walker->nr] = black;
+    //*out << Verbose(1) << "Coloring Walker " << Walker->Name << " black." << endl;
+  }
+  //CleanAccountingLists(TouchedStack, PredecessorList, ShortestPathList, ColorList);
+
+  Free(&PredecessorList);
+  Free(&ShortestPathList);
+  Free(&ColorList);
+  delete(BFSStack);
+};
+
+void AssignRingSizetoNonCycleMembers(ofstream *out, int *&MinimumRingSize, int &MinRingSize, int &NumCycles, molecule *mol, bond *&BackEdge)
+{
+  atom *Root= NULL;
+  atom *Walker = NULL;
+  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
+        Walker = Root;
+
+        //*out << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl;
+        BFSToNextCycle(out, Root, Walker, BackEdge, MinimumRingSize, mol->AtomCount);
+
+      }
+      *out << Verbose(1) << "Minimum ring size of " << *Root << " is " << MinimumRingSize[Root->GetTrueFather()->nr] << "." << endl;
+    }
+    *out << Verbose(1) << "Minimum ring size is " << MinRingSize << ", over " << NumCycles << " cycles total." << endl;
+  } else
+    *out << Verbose(1) << "No rings were detected in the molecular structure." << endl;
+};
+
 /** Analyses the cycles found and returns minimum of all cycle lengths.
  * We begin with a list of Back edges found during DepthFirstSearchAnalysis(). We go through this list - one end is the Root,
@@ -511,14 +720,12 @@
   class StackClass<atom *> *BFSStack = new StackClass<atom *> (AtomCount);   // will hold the current ring
   class StackClass<atom *> *TouchedStack = new StackClass<atom *> (AtomCount);   // contains all "touched" atoms (that need to be reset after BFS loop)
-  atom *Walker = NULL, *OtherAtom = NULL, *Root = NULL;
-  bond *Binder = NULL, *BackEdge = NULL;
-  int RingSize, NumCycles, MinRingSize = -1;
-
-  // initialise each vertex as white with no predecessor, empty queue, color Root lightgray
-  for (int i=AtomCount;i--;) {
-    PredecessorList[i] = NULL;
-    ShortestPathList[i] = -1;
-    ColorList[i] = white;
-  }
+  atom *Walker = NULL;
+  atom *OtherAtom = NULL;
+  atom *Root = NULL;
+  bond *BackEdge = NULL;
+  int NumCycles = 0;
+  int MinRingSize = -1;
+
+  InitializeAccounting(PredecessorList, ShortestPathList, ColorList, AtomCount);
 
   *out << Verbose(1) << "Back edge list - ";
@@ -533,172 +740,22 @@
     // this is the source point
     Walker = BackEdge->rightatom;
-    ShortestPathList[Walker->nr] = 0;
-    BFSStack->ClearStack();  // start with empty BFS stack
-    BFSStack->Push(Walker);
-    TouchedStack->Push(Walker);
+
+    ResetAccounting(Walker, TouchedStack, ShortestPathList, BFSStack);
+
     *out << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl;
     OtherAtom = NULL;
-    do {  // look for Root
-      Walker = BFSStack->PopFirst();
-      *out << Verbose(2) << "Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "." << endl;
-      for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
-        if ((*Runner) != BackEdge) { // only walk along DFS spanning tree (otherwise we always find SP of one being backedge Binder)
-          OtherAtom = (*Runner)->GetOtherAtom(Walker);
-#ifdef ADDHYDROGEN
-          if (OtherAtom->type->Z != 1) {
-#endif
-            *out << Verbose(2) << "Current OtherAtom is: " << OtherAtom->Name << " for bond " << *Binder << "." << endl;
-            if (ColorList[OtherAtom->nr] == white) {
-              TouchedStack->Push(OtherAtom);
-              ColorList[OtherAtom->nr] = lightgray;
-              PredecessorList[OtherAtom->nr] = Walker;  // Walker is the predecessor
-              ShortestPathList[OtherAtom->nr] = ShortestPathList[Walker->nr]+1;
-              *out << Verbose(2) << "Coloring OtherAtom " << OtherAtom->Name << " lightgray, its predecessor is " << Walker->Name << " and its Shortest Path is " << ShortestPathList[OtherAtom->nr] << " egde(s) long." << endl;
-              //if (ShortestPathList[OtherAtom->nr] < MinimumRingSize[Walker->GetTrueFather()->nr]) { // Check for maximum distance
-                *out << Verbose(3) << "Putting OtherAtom into queue." << endl;
-                BFSStack->Push(OtherAtom);
-              //}
-            } else {
-              *out << Verbose(3) << "Not Adding, has already been visited." << endl;
-            }
-            if (OtherAtom == Root)
-              break;
-#ifdef ADDHYDROGEN
-          } else {
-            *out << Verbose(2) << "Skipping hydrogen atom " << *OtherAtom << "." << endl;
-            ColorList[OtherAtom->nr] = black;
-          }
-#endif
-        } else {
-          *out << Verbose(2) << "Bond " << *Binder << " not Visiting, is the back edge." << endl;
-        }
-      }
-      ColorList[Walker->nr] = black;
-      *out << Verbose(1) << "Coloring Walker " << Walker->Name << " black." << endl;
-      if (OtherAtom == Root) { // if we have found the root, check whether this cycle wasn't already found beforehand
-        // step through predecessor list
-        while (OtherAtom != BackEdge->rightatom) {
-          if (!OtherAtom->GetTrueFather()->IsCyclic)  // if one bond in the loop is not marked as cyclic, we haven't found this cycle yet
-            break;
-          else
-            OtherAtom = PredecessorList[OtherAtom->nr];
-        }
-        if (OtherAtom == BackEdge->rightatom) { // if each atom in found cycle is cyclic, loop's been found before already
-          *out << Verbose(3) << "This cycle was already found before, skipping and removing seeker from search." << endl;\
-          do {
-            OtherAtom = TouchedStack->PopLast();
-            if (PredecessorList[OtherAtom->nr] == Walker) {
-              *out << Verbose(4) << "Removing " << *OtherAtom << " from lists and stacks." << endl;
-              PredecessorList[OtherAtom->nr] = NULL;
-              ShortestPathList[OtherAtom->nr] = -1;
-              ColorList[OtherAtom->nr] = white;
-              BFSStack->RemoveItem(OtherAtom);
-            }
-          } while ((!TouchedStack->IsEmpty()) && (PredecessorList[OtherAtom->nr] == NULL));
-          TouchedStack->Push(OtherAtom);  // last was wrongly popped
-          OtherAtom = BackEdge->rightatom; // set to not Root
-        } else
-          OtherAtom = Root;
-      }
-    } while ((!BFSStack->IsEmpty()) && (OtherAtom != Root) && (OtherAtom != NULL)); // || (ShortestPathList[OtherAtom->nr] < MinimumRingSize[Walker->GetTrueFather()->nr])));
-
-    if (OtherAtom == Root) {
-      // now climb back the predecessor list and thus find the cycle members
-      NumCycles++;
-      RingSize = 1;
-      Root->GetTrueFather()->IsCyclic = true;
-      *out << Verbose(1) << "Found ring contains: ";
-      Walker = Root;
-      while (Walker != BackEdge->rightatom) {
-        *out << Walker->Name << " <-> ";
-        Walker = PredecessorList[Walker->nr];
-        Walker->GetTrueFather()->IsCyclic = true;
-        RingSize++;
-      }
-      *out << Walker->Name << "  with a length of " << RingSize << "." << endl << endl;
-      // walk through all and set MinimumRingSize
-      Walker = Root;
-      MinimumRingSize[Walker->GetTrueFather()->nr] = RingSize;
-      while (Walker != BackEdge->rightatom) {
-        Walker = PredecessorList[Walker->nr];
-        if (RingSize < MinimumRingSize[Walker->GetTrueFather()->nr])
-          MinimumRingSize[Walker->GetTrueFather()->nr] = RingSize;
-      }
-      if ((RingSize < MinRingSize) || (MinRingSize == -1))
-        MinRingSize = RingSize;
-    } else {
-      *out << Verbose(1) << "No ring containing " << *Root << " with length equal to or smaller than " << MinimumRingSize[Walker->GetTrueFather()->nr] << " found." << endl;
-    }
-
-    // now clean the lists
-    while (!TouchedStack->IsEmpty()){
-      Walker = TouchedStack->PopFirst();
-      PredecessorList[Walker->nr] = NULL;
-      ShortestPathList[Walker->nr] = -1;
-      ColorList[Walker->nr] = white;
-    }
-  }
-  if (MinRingSize != -1) {
-    // go over all atoms
-    Root = start;
-    while(Root->next != end) {
-      Root = Root->next;
-
-      if (MinimumRingSize[Root->GetTrueFather()->nr] == AtomCount) { // check whether MinimumRingSize is set, if not BFS to next where it is
-        Walker = Root;
-        ShortestPathList[Walker->nr] = 0;
-        BFSStack->ClearStack();  // start with empty BFS stack
-        BFSStack->Push(Walker);
-        TouchedStack->Push(Walker);
-        //*out << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl;
-        OtherAtom = Walker;
-        while (OtherAtom != NULL) {  // look for Root
-          Walker = BFSStack->PopFirst();
-          //*out << Verbose(2) << "Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "." << endl;
-          for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
-            if (((*Runner) != BackEdge) || (Walker->ListOfBonds.size() == 1)) { // only walk along DFS spanning tree (otherwise we always find SP of 1 being backedge Binder), but terminal hydrogens may be connected via backedge, hence extra check
-              OtherAtom = (*Runner)->GetOtherAtom(Walker);
-              //*out << Verbose(2) << "Current OtherAtom is: " << OtherAtom->Name << " for bond " << *Binder << "." << endl;
-              if (ColorList[OtherAtom->nr] == white) {
-                TouchedStack->Push(OtherAtom);
-                ColorList[OtherAtom->nr] = lightgray;
-                PredecessorList[OtherAtom->nr] = Walker;  // Walker is the predecessor
-                ShortestPathList[OtherAtom->nr] = ShortestPathList[Walker->nr]+1;
-                //*out << Verbose(2) << "Coloring OtherAtom " << OtherAtom->Name << " lightgray, its predecessor is " << Walker->Name << " and its Shortest Path is " << ShortestPathList[OtherAtom->nr] << " egde(s) long." << endl;
-                if (OtherAtom->GetTrueFather()->IsCyclic) { // if the other atom is connected to a ring
-                  MinimumRingSize[Root->GetTrueFather()->nr] = ShortestPathList[OtherAtom->nr]+MinimumRingSize[OtherAtom->GetTrueFather()->nr];
-                  OtherAtom = NULL; //break;
-                  break;
-                } else
-                  BFSStack->Push(OtherAtom);
-              } else {
-                //*out << Verbose(3) << "Not Adding, has already been visited." << endl;
-              }
-            } else {
-              //*out << Verbose(3) << "Not Visiting, is a back edge." << endl;
-            }
-          }
-          ColorList[Walker->nr] = black;
-          //*out << Verbose(1) << "Coloring Walker " << Walker->Name << " black." << endl;
-        }
-
-        // now clean the lists
-        while (!TouchedStack->IsEmpty()){
-          Walker = TouchedStack->PopFirst();
-          PredecessorList[Walker->nr] = NULL;
-          ShortestPathList[Walker->nr] = -1;
-          ColorList[Walker->nr] = white;
-        }
-      }
-      *out << Verbose(1) << "Minimum ring size of " << *Root << " is " << MinimumRingSize[Root->GetTrueFather()->nr] << "." << endl;
-    }
-    *out << Verbose(1) << "Minimum ring size is " << MinRingSize << ", over " << NumCycles << " cycles total." << endl;
-  } else
-    *out << Verbose(1) << "No rings were detected in the molecular structure." << endl;
-
+    CyclicBFSFromRootToRoot(out, Root, BackEdge, TouchedStack, ShortestPathList, PredecessorList, BFSStack, ColorList);
+
+    RetrieveCycleMembers(out, Root, OtherAtom, BackEdge, PredecessorList, MinimumRingSize, MinRingSize);
+
+    CleanAccounting(TouchedStack, PredecessorList, ShortestPathList, ColorList);
+  }
   Free(&PredecessorList);
   Free(&ShortestPathList);
   Free(&ColorList);
   delete(BFSStack);
+
+  AssignRingSizetoNonCycleMembers(out, MinimumRingSize, MinRingSize, NumCycles, this, BackEdge);
+
 };
 
