Index: src/Graph/CyclicStructureAnalysis.cpp
===================================================================
--- src/Graph/CyclicStructureAnalysis.cpp	(revision fb9f6d8c6e2f0362287b7c20b8f7678c84e2d2d2)
+++ src/Graph/CyclicStructureAnalysis.cpp	(revision 8dbcafdbf694a42e92d2e08e97d4aa979751714d)
@@ -99,16 +99,15 @@
 
 /** Performs a BFS from \a *Root, trying to find the same node and hence a cycle.
+ * \param OtherAtom pointing to Root on return indicating found cycle
  * \param *&BackEdge the edge from root that we don't want to move along
- * \param &BFS accounting structure
- */
-void CyclicStructureAnalysis::CyclicBFSFromRootToRoot(bond::ptr &BackEdge)
+ */
+void CyclicStructureAnalysis::CyclicBFSFromRootToRoot(atom *&OtherAtom, bond::ptr &BackEdge)
 {
   atom *Walker = NULL;
-  atom *OtherAtom = NULL;
   do { // look for Root
     ASSERT(!BFSStack.empty(), "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - BFSStack is empty!");
     Walker = BFSStack.front();
     BFSStack.pop_front();
-    LOG(2, "INFO: Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "." << endl);
+    LOG(2, "INFO: Current Walker is " << *Walker << ", we look for SP to Root " << *Root << ".");
     const BondList& ListOfBonds = Walker->getListOfBonds();
     for (BondList::const_iterator Runner = ListOfBonds.begin();
@@ -118,5 +117,5 @@
         OtherAtom = (*Runner)->GetOtherAtom(Walker);
         if ((treatment == IncludeHydrogen) || (OtherAtom->getType()->getAtomicNumber() != 1)) {
-          LOG(2, "INFO: Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << "." << endl);
+          LOG(2, "INFO: Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << ".");
           if (ColorList[OtherAtom->getNr()] == GraphEdge::white) {
             TouchedStack.push_front(OtherAtom);
@@ -124,34 +123,38 @@
             PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor
             ShortestPathList[OtherAtom->getNr()] = ShortestPathList[Walker->getNr()] + 1;
-            LOG(2, "INFO: Coloring OtherAtom " << OtherAtom->getName() << " lightgray, its predecessor is " << Walker->getName() << " and its Shortest Path is " << ShortestPathList[OtherAtom->getNr()] << " egde(s) long." << endl);
+            LOG(2, "INFO: Coloring OtherAtom " << OtherAtom->getName() << " lightgray, its predecessor is " << Walker->getName() << " and its Shortest Path is " << ShortestPathList[OtherAtom->getNr()] << " egde(s) long.");
             //if (ShortestPathList[OtherAtom->getNr()] < MinimumRingSize[Walker->GetTrueFather()->getNr()]) { // Check for maximum distance
-            LOG(3, "ACCEPT: Putting OtherAtom into queue." << endl);
+            LOG(3, "ACCEPT: Putting OtherAtom " << OtherAtom->getName() << " into queue.");
             BFSStack.push_front(OtherAtom);
             //}
           } else {
-            LOG(3, "REJECT: Not Adding, has already been visited." << endl);
+            LOG(3, "REJECT: Not Adding, has already been visited.");
           }
           if (OtherAtom == Root)
             break;
         } else {
-          LOG(2, "INFO: Skipping hydrogen atom " << *OtherAtom << "." << endl);
+          LOG(2, "INFO: Skipping hydrogen atom " << *OtherAtom << ".");
           ColorList[OtherAtom->getNr()] = GraphEdge::black;
         }
       } else {
-        LOG(2, "REJECT: Bond " << *(*Runner) << " not Visiting, is the back edge." << endl);
+        LOG(2, "REJECT: Bond " << *(*Runner) << " not Visiting, is the back edge.");
       }
     }
     ColorList[Walker->getNr()] = GraphEdge::black;
-    LOG(1, "INFO: Coloring Walker " << Walker->getName() << " " << GraphEdge::getColorName(ColorList[Walker->getNr()]) << "." << endl);
+    LOG(1, "INFO: Coloring Walker " << Walker->getName() << " " << GraphEdge::getColorName(ColorList[Walker->getNr()]) << ".");
     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
+      LOG(4, "DEBUG: Checking whether all predecessors are already marked cyclic ...");
+      while (OtherAtom != BackEdge->rightatom) {  // Note that leftatom is Root itself
+        if (!OtherAtom->GetTrueFather()->IsCyclic) { // if one bond in the loop is not marked as cyclic, we haven't found this cycle yet
+          LOG(4, "\tDEBUG: OtherAtom " << *OtherAtom << " is not cyclic, breaking.");
           break;
-        else
+        } else
           OtherAtom = PredecessorList[OtherAtom->getNr()];
       }
-      if (OtherAtom == BackEdge->rightatom) { // if each atom in found cycle is cyclic, loop's been found before already
-        LOG(3, "INFO This cycle was already found before, skipping and removing seeker from search." << endl);
+      LOG(4, "DEBUG: Checking done.");
+      // if each atom in found cycle is cyclic, loop's been found before already
+      if (OtherAtom == BackEdge->rightatom) { // loop got round completely
+        LOG(3, "INFO: This cycle was already found before, skipping and removing seeker from search.");
         do {
           ASSERT(!TouchedStack.empty(), "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - TouchedStack is empty!");
@@ -159,5 +162,5 @@
           TouchedStack.pop_front();
           if (PredecessorList[OtherAtom->getNr()] == Walker) {
-            LOG(4, "INFO: Removing " << *OtherAtom << " from lists and stacks." << endl);
+            LOG(4, "INFO: Removing " << *OtherAtom << " from lists and stacks.");
             PredecessorList[OtherAtom->getNr()] = NULL;
             ShortestPathList[OtherAtom->getNr()] = -1;
@@ -175,6 +178,8 @@
         TouchedStack.push_front(OtherAtom); // last was wrongly popped
         OtherAtom = BackEdge->rightatom; // set to not Root
-      } else
+      } else {
         OtherAtom = Root;
+        LOG(2, "INFO: We have reached Root " << *OtherAtom << " and may extract the cycle.");
+      }
     }
   } while ((!BFSStack.empty()) && (OtherAtom != Root) && (OtherAtom != NULL)); // || (ShortestPathList[OtherAtom->getNr()] < MinimumRingSize[Walker->GetTrueFather()->getNr()])));
@@ -186,9 +191,13 @@
  * \param &BFS accounting structure
  * \param &MinRingSize global minimum distance from one node without encountering oneself, set on return
- */
-void CyclicStructureAnalysis::RetrieveCycleMembers(atom *&OtherAtom, bond::ptr &BackEdge, int &MinRingSize)
+ * \param &NumCyles number of cycles in graph
+ */
+void CyclicStructureAnalysis::RetrieveCycleMembers(
+    atom *&OtherAtom,
+    bond::ptr &BackEdge,
+    int &MinRingSize,
+    int &NumCycles)
 {
   atom *Walker = NULL;
-  int NumCycles = 0;
   int RingSize = -1;
 
@@ -213,13 +222,16 @@
     // walk through all and set MinimumRingSize
     Walker = Root;
-    ASSERT(!MinimumRingSize.count(Walker->GetTrueFather()->getNr()),
-        "CyclicStructureAnalysis::RetrieveCycleMembers() - setting MinimumRingSize of "
-        +toString(*(Walker->GetTrueFather()))+" to "
-        +toString(RingSize)+" which is already set to "
-        +toString(MinimumRingSize[Walker->GetTrueFather()->getNr()])+".");
-    MinimumRingSize[Walker->GetTrueFather()->getNr()] = RingSize;
-    while (Walker != BackEdge->rightatom) {
+    if ((MinimumRingSize.count(Walker->GetTrueFather()->getNr()) == 0)
+        || (RingSize < MinimumRingSize[Walker->GetTrueFather()->getNr()])) {
+      MinimumRingSize[Walker->GetTrueFather()->getNr()] = RingSize;
+    } else {
+      LOG(3, "INFO: Not setting MinimumRingSize of "<< *(Walker->GetTrueFather())
+          << " to " << RingSize << " which is already set to "
+          << MinimumRingSize[Walker->GetTrueFather()->getNr()] << ".");
+    }
+    while (Walker != BackEdge->rightatom) { // note that Root is leftatom
       Walker = PredecessorList[Walker->getNr()];
-      if (RingSize < MinimumRingSize[Walker->GetTrueFather()->getNr()])
+      if ((MinimumRingSize.count(Walker->GetTrueFather()->getNr()) == 0)
+          || (RingSize < MinimumRingSize[Walker->GetTrueFather()->getNr()]))
         MinimumRingSize[Walker->GetTrueFather()->getNr()] = RingSize;
     }
@@ -227,14 +239,15 @@
       MinRingSize = RingSize;
   } else {
-    LOG(1, "INFO: No ring containing " << *Root << " with length equal to or smaller than " << MinimumRingSize[Root->GetTrueFather()->getNr()] << " found." << endl);
+    LOG(1, "INFO: No ring containing " << *Root << " with length equal to or smaller than " << MinimumRingSize[Root->GetTrueFather()->getNr()] << " found.");
   }
 }
 
 /** From a given node performs a BFS to touch the next cycle, for whose nodes \a MinimumRingSize is set and set it accordingly.
- * \param *&Root node to look for closest cycle from, i.e. \a MinimumRingSize is set for this node
+ * \param *&Walker node to look for closest cycle from, i.e. \a MinimumRingSize is set for this node
  * \param AtomCount number of nodes in graph
  */
-void CyclicStructureAnalysis::BFSToNextCycle(atom *&Root, atom *&Walker)
-{
+void CyclicStructureAnalysis::BFSToNextCycle(atom *Walker)
+{
+  atom *Root = Walker;
   atom *OtherAtom = Walker;
 
@@ -246,5 +259,5 @@
     Walker = BFSStack.front();
     BFSStack.pop_front();
-    LOG(2, "INFO: Current Walker is " << *Walker << ", we look for SP to Root " << *Root << ".");
+    LOG(2, "INFO: Current Walker is " << *Walker << ", BFS-stepping away from Root " << *Root << ".");
     const BondList& ListOfBonds = Walker->getListOfBonds();
     for (BondList::const_iterator Runner = ListOfBonds.begin();
@@ -252,31 +265,51 @@
         ++Runner) {
       // "removed (*Runner) != BackEdge) || " from next if, is u
-      if ((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
+
+      // 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
+//      if ((ListOfBonds.size() != 1)) {
         OtherAtom = (*Runner)->GetOtherAtom(Walker);
-        LOG(2, "INFO: Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << ".");
-        if (ColorList[OtherAtom->getNr()] == GraphEdge::white) {
-          TouchedStack.push_front(OtherAtom);
-          ColorList[OtherAtom->getNr()] = GraphEdge::lightgray;
-          PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor
-          ShortestPathList[OtherAtom->getNr()] = ShortestPathList[Walker->getNr()] + 1;
-          LOG(2, "ACCEPT: Coloring OtherAtom " << OtherAtom->getName() << " lightgray, its predecessor is " << Walker->getName() << " and its Shortest Path is " << ShortestPathList[OtherAtom->getNr()] << " egde(s) long.");
-          if (OtherAtom->GetTrueFather()->IsCyclic) { // if the other atom is connected to a ring
-            ASSERT(!MinimumRingSize.count(Root->GetTrueFather()->getNr()),
-                "CyclicStructureAnalysis::BFSToNextCycle() - setting MinimumRingSize of "
-                +toString(*(Root->GetTrueFather()))+" to "+
-                toString(ShortestPathList[OtherAtom->getNr()] + MinimumRingSize[OtherAtom->GetTrueFather()->getNr()])
-                +" which is already set to "
-                +toString(MinimumRingSize[Root->GetTrueFather()->getNr()])+".");
-            MinimumRingSize[Root->GetTrueFather()->getNr()] = ShortestPathList[OtherAtom->getNr()] + MinimumRingSize[OtherAtom->GetTrueFather()->getNr()];
-            OtherAtom = NULL; //break;
-            break;
-          } else
-            BFSStack.push_front(OtherAtom);
+        if ((treatment == IncludeHydrogen) || (OtherAtom->getType()->getAtomicNumber() != 1)) {
+          LOG(2, "INFO: Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << ".");
+          if (ColorList[OtherAtom->getNr()] == GraphEdge::white) {
+            TouchedStack.push_front(OtherAtom);
+            ColorList[OtherAtom->getNr()] = GraphEdge::lightgray;
+            PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor
+            ShortestPathList[OtherAtom->getNr()] = ShortestPathList[Walker->getNr()] + 1;
+            LOG(2, "ACCEPT: Coloring OtherAtom "
+                << OtherAtom->getName() << " lightgray, its predecessor is "
+                << Walker->getName() << " and its Shortest Path is "
+                << ShortestPathList[OtherAtom->getNr()] << " egde(s) long.");
+            // distance is a locally optimal criterion (we have eliminated all
+            // cycles already). Hence, we may assume that all set MinimumRingSize
+            // correspond to shortest distances to cycles. I.e., as soon as we reach
+            // as set MinimumRingSize we may use it and the current shortest path
+            // distance to it
+            if (MinimumRingSize.count(OtherAtom->GetTrueFather()->getNr())) {
+              LOG(2, "SUCCESS: Found set MinimumRingSize at " << *OtherAtom
+                  << ", walking back to Root " << *Root << ".");
+              // set all predecessors
+              const unsigned int shorttestpath = ShortestPathList[OtherAtom->getNr()];
+              atom *Backwalker = OtherAtom;
+              while (Backwalker != Root) {
+                Backwalker = PredecessorList[Backwalker->getNr()];
+                MinimumRingSize[Backwalker->GetTrueFather()->getNr()] =
+                    (shorttestpath - ShortestPathList[Backwalker->getNr()])
+                    + MinimumRingSize[OtherAtom->GetTrueFather()->getNr()];
+                LOG(2, "Setting MinimumRingSize of " << *Backwalker << " to "
+                    << MinimumRingSize[Backwalker->GetTrueFather()->getNr()] << ".");
+              }
+              OtherAtom = NULL; //break;
+              break;
+            } else
+              BFSStack.push_front(OtherAtom);
+          } else {
+            LOG(3, "REJECT: Not Adding, has already been visited.");
+          }
         } else {
-          LOG(3, "REJECT: Not Adding, has already been visited.");
+          LOG(3, "REJECT: Not Visiting, is a back edge to hydrogen.");
         }
-      } else {
-        LOG(3, "REJECT: Not Visiting, is a back edge.");
-      }
+//      }
     }
     ColorList[Walker->getNr()] = GraphEdge::black;
@@ -285,12 +318,11 @@
 }
 
-/** All nodes that are not in cycles get assigned a \a *&MinimumRingSizeby BFS to next cycle.
- * \param *&MinimumRingSize array with minimum distance without encountering onself for each atom
- * \param &MinRingSize global minium distance
- * \param &NumCyles number of cycles in graph
- */
-void CyclicStructureAnalysis::AssignRingSizetoNonCycleMembers(int &MinRingSize, int &NumCycles)
-{
-  atom *Root = NULL;
+/** All nodes that are not in cycles get assigned a \a *&MinimumRingSize by BFS to next cycle.
+ * \param *&MinimumRingSize array with minimum distance without encountering oneself for each atom
+ * \param MinRingSize global minium distance
+ * \param NumCyles number of cycles in graph
+ */
+void CyclicStructureAnalysis::AssignRingSizetoNonCycleMembers(const int MinRingSize, const int NumCycles)
+{
   atom *Walker = NULL;
   if (MinRingSize != -1) { // if rings are present
@@ -300,21 +332,18 @@
         iter != allatoms.end();
         ++iter) {
-      Root = *iter;
-
-      if (MinimumRingSize.find(Root->GetTrueFather()->getNr()) != MinimumRingSize.end()) { // check whether MinimumRingSize is set, if not BFS to next where it is
-        Walker = Root;
-
+      Walker = *iter;
+
+      if (MinimumRingSize.find(Walker->GetTrueFather()->getNr()) == MinimumRingSize.end()) { // check whether MinimumRingSize is set, if not BFS to next where it is
         LOG(1, "---------------------------------------------------------------------------------------------------------");
-        BFSToNextCycle(Root, Walker);
-
+        BFSToNextCycle(Walker);
       }
-      ASSERT(MinimumRingSize.find(Root->GetTrueFather()->getNr()) != MinimumRingSize.end(),
+      ASSERT(MinimumRingSize.find(Walker->GetTrueFather()->getNr()) != MinimumRingSize.end(),
           "CyclicStructureAnalysis::AssignRingSizetoNonCycleMembers() - BFSToNextCycle did not set MinimumRingSize of "
-          +toString(*(Root->GetTrueFather()))+".");
-      LOG(1, "INFO: Minimum ring size of " << *Root << " is " << MinimumRingSize[Root->GetTrueFather()->getNr()] << "." << endl);
-    }
-    LOG(1, "INFO: Minimum ring size is " << MinRingSize << ", over " << NumCycles << " cycles total." << endl);
+          +toString(*(Walker->GetTrueFather()))+".");
+      LOG(1, "INFO: Minimum ring size of " << *Walker << " is " << MinimumRingSize[Walker->GetTrueFather()->getNr()] << ".");
+    }
+    LOG(1, "INFO: Minimum ring size is " << MinRingSize << ", over " << NumCycles << " cycle(s) total.");
   } else
-    LOG(1, "INFO: No rings were detected in the molecular structure." << endl);
+    LOG(1, "INFO: No rings were detected in the molecular structure.");
 }
 
@@ -336,10 +365,14 @@
   int MinRingSize = -1;
 
-  //std::stringstream output;
-  //output << "Back edge list - ";
-  //BackEdgeStack->Output(output);
-  //LOG(0, output.str());
-
-  LOG(1, "STATUS: Analysing cycles ... " << endl);
+  {
+    std::stringstream output;
+    output << "Back edge list - ";
+    for (std::deque<bond::ptr >::const_iterator iter = BackEdgeStack->begin();
+        iter != BackEdgeStack->end(); ++iter)
+      output << **iter << " ";
+    LOG(0, output.str());
+  }
+
+  LOG(1, "STATUS: Analysing cycles ... ");
   NumCycles = 0;
   while (!BackEdgeStack->empty()) {
@@ -353,10 +386,10 @@
     InitializeToRoot(Walker);
 
-    LOG(1, "---------------------------------------------------------------------------------------------------------" << endl);
+    LOG(1, "---------------------------------------------------------------------------------------------------------");
     OtherAtom = NULL;
     // go to next cycle via BFS
-    CyclicBFSFromRootToRoot(BackEdge);
+    CyclicBFSFromRootToRoot(OtherAtom, BackEdge);
     // get all member nodes of this cycle
-    RetrieveCycleMembers(OtherAtom, BackEdge, MinRingSize);
+    RetrieveCycleMembers(OtherAtom, BackEdge, MinRingSize, NumCycles);
 
     CleanAllTouched();
Index: src/Graph/CyclicStructureAnalysis.hpp
===================================================================
--- src/Graph/CyclicStructureAnalysis.hpp	(revision fb9f6d8c6e2f0362287b7c20b8f7678c84e2d2d2)
+++ src/Graph/CyclicStructureAnalysis.hpp	(revision 8dbcafdbf694a42e92d2e08e97d4aa979751714d)
@@ -43,8 +43,8 @@
   void InitializeToRoot(atom *&Walker);
   // performing tasks
-  void CyclicBFSFromRootToRoot(bond::ptr &BackEdge);
-  void RetrieveCycleMembers(atom *&OtherAtom, bond::ptr &BackEdge, int &MinRingSize);
-  void BFSToNextCycle(atom *&Root, atom *&Walker);
-  void AssignRingSizetoNonCycleMembers(int &MinRingSize, int &NumCycles);
+  void CyclicBFSFromRootToRoot(atom *&OtherAtom, bond::ptr &BackEdge);
+  void RetrieveCycleMembers(atom *&OtherAtom, bond::ptr &BackEdge, int &MinRingSize, int &NumCycles);
+  void BFSToNextCycle(atom *Walker);
+  void AssignRingSizetoNonCycleMembers(const int MinRingSize, const int NumCycles);
   // output
   void OutputAlreadyVisited(int *list);
Index: src/Graph/DepthFirstSearchAnalysis.cpp
===================================================================
--- src/Graph/DepthFirstSearchAnalysis.cpp	(revision fb9f6d8c6e2f0362287b7c20b8f7678c84e2d2d2)
+++ src/Graph/DepthFirstSearchAnalysis.cpp	(revision 8dbcafdbf694a42e92d2e08e97d4aa979751714d)
@@ -130,14 +130,15 @@
     return false;
   }
-  bond::ptr Binder = BackEdgeStack.front();
-  bond::ptr FirstBond = Binder; // mark the first bond, so that we don't loop through the stack indefinitely
-  atom *Walker = NULL, *OtherAtom = NULL;
+  std::deque<bond::ptr > MyBackEdgeStack = BackEdgeStack;
 
   do { // go through all bonds and push local ones
+    const bond::ptr &Binder = MyBackEdgeStack.front(); // loop the stack for next item
+    MyBackEdgeStack.pop_front();
+    LOG(3, "INFO: Current candidate edge " << *Binder << ".");
     const ListOfLocalAtoms_t::const_iterator leftiter = ListOfLocalAtoms.find(Binder->leftatom->getNr());
     ASSERT( leftiter != ListOfLocalAtoms.end(),
         "DepthFirstSearchAnalysis::PickLocalBackEdges() - could not find atom id "
         +toString(Binder->leftatom->getNr())+" in ListOfLocalAtoms.");
-    Walker = leftiter->second; // get one atom in the reference molecule
+    atom * const Walker = leftiter->second; // get one atom in the reference molecule
     if (Walker != NULL) { // if this Walker exists in the subgraph ...
       const BondList& ListOfBonds = Walker->getListOfBonds();
@@ -145,5 +146,5 @@
           Runner != ListOfBonds.end();
           ++Runner) {
-        OtherAtom = (*Runner)->GetOtherAtom(Walker);
+        atom * const OtherAtom = (*Runner)->GetOtherAtom(Walker);
         const ListOfLocalAtoms_t::const_iterator rightiter = ListOfLocalAtoms.find((*Runner)->rightatom->getNr());
         if (OtherAtom == rightiter->second) { // found the bond
@@ -154,8 +155,5 @@
       }
     }
-    ASSERT(!BackEdgeStack.empty(), "DepthFirstSearchAnalysis::PickLocalBackEdges() - ReferenceStack is empty!");
-    Binder = BackEdgeStack.front(); // loop the stack for next item
-    LOG(3, "Current candidate edge " << Binder << ".");
-  } while (FirstBond != Binder);
+  } while (!MyBackEdgeStack.empty());
 
   return status;
