Index: src/molecule_fragmentation.cpp
===================================================================
--- src/molecule_fragmentation.cpp	(revision 14e73aa8344b12c5bad0a9b1eaaffda56d596001)
+++ src/molecule_fragmentation.cpp	(revision 407536f92b012b8b92b2dff235834903e8b9565b)
@@ -1156,5 +1156,40 @@
 };
 
-/** Initializes FragmenSearch.
+/** Allocates memory for UniqueFragments::BondsPerSPList.
+ * \param *out output stream
+ * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
+ * \param FragmentSearch UniqueFragments
+ * \sa FreeSPList()
+ */
+void InitialiseSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
+{
+  FragmentSearch.BondsPerSPList = Malloc<bond*>(Order * 2, "molecule::PowerSetGenerator: ***BondsPerSPList");
+  FragmentSearch.BondsPerSPCount = Malloc<int>(Order, "molecule::PowerSetGenerator: *BondsPerSPCount");
+  for (int i=Order;i--;) {
+    FragmentSearch.BondsPerSPList[2*i] = new bond();    // start node
+    FragmentSearch.BondsPerSPList[2*i+1] = new bond();  // end node
+    FragmentSearch.BondsPerSPList[2*i]->next = FragmentSearch.BondsPerSPList[2*i+1];     // intertwine these two
+    FragmentSearch.BondsPerSPList[2*i+1]->previous = FragmentSearch.BondsPerSPList[2*i];
+    FragmentSearch.BondsPerSPCount[i] = 0;
+  }
+};
+
+/** Free's memory for for UniqueFragments::BondsPerSPList.
+ * \param *out output stream
+ * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
+ * \param FragmentSearch UniqueFragments\
+ * \sa InitialiseSPList()
+ */
+void FreeSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
+{
+  Free(&FragmentSearch.BondsPerSPCount);
+  for (int i=Order;i--;) {
+    delete(FragmentSearch.BondsPerSPList[2*i]);
+    delete(FragmentSearch.BondsPerSPList[2*i+1]);
+  }
+  Free(&FragmentSearch.BondsPerSPList);
+};
+
+/** Sets FragmenSearch to initial value.
  * Sets UniqueFragments::ShortestPathList entries to zero, UniqueFragments::BondsPerSPCount to zero (except zero level to 1) and
  * adds initial bond UniqueFragments::Root to UniqueFragments::Root to UniqueFragments::BondsPerSPList
@@ -1164,5 +1199,5 @@
  * \sa FreeSPList()
  */
-void InitialiseSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
+void SetSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
 {
   // prepare Label and SP arrays of the BFS search
@@ -1183,5 +1218,5 @@
  * \sa InitialiseSPList()
  */
-void FreeSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
+void ResetSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
 {
   bond *Binder = NULL;
@@ -1341,5 +1376,5 @@
   *out << Verbose(0) << "Begin of PowerSetGenerator with order " << Order << " at Root " << *FragmentSearch.Root << "." << endl;
 
-  InitialiseSPList(out, Order, FragmentSearch);
+  SetSPList(out, Order, FragmentSearch);
 
   // do a BFS search to fill the SP lists and label the found vertices
@@ -1375,5 +1410,5 @@
 
   // free'ing the bonds lists
-  FreeSPList(out, Order, FragmentSearch);
+  ResetSPList(out, Order, FragmentSearch);
 
   // return list
@@ -1408,4 +1443,72 @@
 
 
+/** Combines all KeySets from all orders into single ones (with just unique entries).
+ * \param *out output stream for debugging
+ * \param *&FragmentList list to fill
+ * \param ***FragmentLowerOrdersList
+ * \param &RootStack stack with all root candidates (unequal to each atom in complete molecule if adaptive scheme is applied)
+ * \param *mol molecule with atoms and bonds
+ */
+int CombineAllOrderListIntoOne(ofstream *out, Graph *&FragmentList, Graph ***FragmentLowerOrdersList, KeyStack &RootStack, molecule *mol)
+{
+  int RootNr = 0;
+  int RootKeyNr = 0;
+  int counter = 0;
+  int NumLevels = 0;
+  atom *Walker = NULL;
+
+  *out << Verbose(0) << "Combining the lists of all orders per order and finally into a single one." << endl;
+  if (FragmentList == NULL) {
+    FragmentList = new Graph;
+    counter = 0;
+  } else {
+    counter = FragmentList->size();
+  }
+  while (!RootStack.empty()) {
+    RootKeyNr = RootStack.front();
+    RootStack.pop_front();
+    Walker = mol->FindAtom(RootKeyNr);
+    NumLevels = 1 << (Walker->AdaptiveOrder - 1);
+    for(int i=0;i<NumLevels;i++) {
+      if (FragmentLowerOrdersList[RootNr][i] != NULL) {
+        InsertGraphIntoGraph(out, *FragmentList, (*FragmentLowerOrdersList[RootNr][i]), &counter);
+      }
+    }
+    RootStack.push_back(Walker->nr);
+    RootNr++;
+  }
+  return counter;
+};
+
+/** Free's memory allocated for all KeySets from all orders.
+ * \param *out output stream for debugging
+ * \param ***FragmentLowerOrdersList
+ * \param &RootStack stack with all root candidates (unequal to each atom in complete molecule if adaptive scheme is applied)
+ * \param *mol molecule with atoms and bonds
+ */
+void FreeAllOrdersList(ofstream *out, Graph ***FragmentLowerOrdersList, KeyStack &RootStack, molecule *mol)
+{
+  *out << Verbose(1) << "Free'ing the lists of all orders per order." << endl;
+  int RootNr = 0;
+  int RootKeyNr = 0;
+  int NumLevels = 0;
+  atom *Walker = NULL;
+  while (!RootStack.empty()) {
+    RootKeyNr = RootStack.front();
+    RootStack.pop_front();
+    Walker = mol->FindAtom(RootKeyNr);
+    NumLevels = 1 << (Walker->AdaptiveOrder - 1);
+    for(int i=0;i<NumLevels;i++) {
+      if (FragmentLowerOrdersList[RootNr][i] != NULL) {
+        delete(FragmentLowerOrdersList[RootNr][i]);
+      }
+    }
+    Free(&FragmentLowerOrdersList[RootNr]);
+    RootNr++;
+  }
+  Free(&FragmentLowerOrdersList);
+};
+
+
 /** Performs BOSSANOVA decomposition at selected sites, increasing the cutoff by one at these sites.
  * -# constructs a complete keyset of the molecule
@@ -1428,9 +1531,13 @@
 {
   Graph ***FragmentLowerOrdersList = NULL;
-  int NumLevels, NumMolecules, TotalNumMolecules = 0, *NumMoleculesOfOrder = NULL;
-  int counter = 0, Order;
+  int NumLevels = 0;
+  int NumMolecules = 0;
+  int TotalNumMolecules = 0;
+  int *NumMoleculesOfOrder = NULL;
+  int Order = 0;
   int UpgradeCount = RootStack.size();
   KeyStack FragmentRootStack;
-  int RootKeyNr, RootNr;
+  int RootKeyNr = 0;
+  int RootNr = 0;
   struct UniqueFragments FragmentSearch;
 
@@ -1478,13 +1585,5 @@
 
       // initialise Order-dependent entries of UniqueFragments structure
-      FragmentSearch.BondsPerSPList = Malloc<bond*>(Order * 2, "molecule::PowerSetGenerator: ***BondsPerSPList");
-      FragmentSearch.BondsPerSPCount = Malloc<int>(Order, "molecule::PowerSetGenerator: *BondsPerSPCount");
-      for (int i=Order;i--;) {
-        FragmentSearch.BondsPerSPList[2*i] = new bond();    // start node
-        FragmentSearch.BondsPerSPList[2*i+1] = new bond();  // end node
-        FragmentSearch.BondsPerSPList[2*i]->next = FragmentSearch.BondsPerSPList[2*i+1];     // intertwine these two
-        FragmentSearch.BondsPerSPList[2*i+1]->previous = FragmentSearch.BondsPerSPList[2*i];
-        FragmentSearch.BondsPerSPCount[i] = 0;
-      }
+      InitialiseSPList(out, Order, FragmentSearch);
 
       // allocate memory for all lower level orders in this 1D-array of ptrs
@@ -1504,48 +1603,11 @@
       FragmentSearch.Root = Walker;
       NumMoleculesOfOrder[RootNr] = PowerSetGenerator(out, Walker->AdaptiveOrder, FragmentSearch, CompleteMolecule);
+
+      // output resulting number
       *out << Verbose(1) << "Number of resulting KeySets is: " << NumMoleculesOfOrder[RootNr] << "." << endl;
       if (NumMoleculesOfOrder[RootNr] != 0) {
         NumMolecules = 0;
-
-        // we don't have to dive into suborders! These keysets are all already created on lower orders!
-        // this was all ancient stuff, when we still depended on the TEFactors (and for those the suborders were needed)
-
-//        if ((NumLevels >> 1) > 0) {
-//          // create lower order fragments
-//          *out << Verbose(0) << "Creating list of unique fragments of lower Bond Order terms to be subtracted." << endl;
-//          Order = Walker->AdaptiveOrder;
-//          for (int source=0;source<(NumLevels >> 1);source++) { // 1-terms don't need any more splitting, that's why only half is gone through (shift again)
-//            // step down to next order at (virtual) boundary of powers of 2 in array
-//            while (source >= (1 << (Walker->AdaptiveOrder-Order))) // (int)pow(2,Walker->AdaptiveOrder-Order))
-//              Order--;
-//            *out << Verbose(0) << "Current Order is: " << Order << "." << endl;
-//            for (int SubOrder=Order-1;SubOrder>0;SubOrder--) {
-//              int dest = source + (1 << (Walker->AdaptiveOrder-(SubOrder+1)));
-//              *out << Verbose(0) << "--------------------------------------------------------------------------------------------------------------" << endl;
-//              *out << Verbose(0) << "Current SubOrder is: " << SubOrder << " with source " << source << " to destination " << dest << "." << endl;
-//
-//              // every molecule is split into a list of again (Order - 1) molecules, while counting all molecules
-//              //*out << Verbose(1) << "Splitting the " << (*FragmentLowerOrdersList[RootNr][source]).size() << " molecules of the " << source << "th cell in the array." << endl;
-//              //NumMolecules = 0;
-//              FragmentLowerOrdersList[RootNr][dest] = new Graph;
-//              for(Graph::iterator runner = (*FragmentLowerOrdersList[RootNr][source]).begin();runner != (*FragmentLowerOrdersList[RootNr][source]).end(); runner++) {
-//                for (KeySet::iterator sprinter = (*runner).first.begin();sprinter != (*runner).first.end(); sprinter++) {
-//                  Graph TempFragmentList;
-//                  FragmentSearch.TEFactor = -(*runner).second.second;
-//                  FragmentSearch.Leaflet = &TempFragmentList;      // set to insertion graph
-//                  FragmentSearch.Root = FindAtom(*sprinter);
-//                  NumMoleculesOfOrder[RootNr] += PowerSetGenerator(out, SubOrder, FragmentSearch, (*runner).first);
-//                  // insert new keysets FragmentList into FragmentLowerOrdersList[Walker->AdaptiveOrder-1][dest]
-//                  *out << Verbose(1) << "Merging resulting key sets with those present in destination " << dest << "." << endl;
-//                  InsertGraphIntoGraph(out, *FragmentLowerOrdersList[RootNr][dest], TempFragmentList, &NumMolecules);
-//                }
-//              }
-//              *out << Verbose(1) << "Number of resulting molecules for SubOrder " << SubOrder << " is: " << NumMolecules << "." << endl;
-//            }
-//          }
-//        }
       } else {
         Walker->GetTrueFather()->MaxOrder = true;
-//        *out << Verbose(1) << "Hence, we don't dive into SubOrders ... " << endl;
       }
       // now, we have completely filled each cell of FragmentLowerOrdersList[] for the current Walker->AdaptiveOrder
@@ -1557,10 +1619,5 @@
 
       // free Order-dependent entries of UniqueFragments structure for next loop cycle
-      Free(&FragmentSearch.BondsPerSPCount);
-      for (int i=Order;i--;) {
-        delete(FragmentSearch.BondsPerSPList[2*i]);
-        delete(FragmentSearch.BondsPerSPList[2*i+1]);
-      }
-      Free(&FragmentSearch.BondsPerSPList);
+      FreeSPList(out, Order, FragmentSearch);
     }
   }
@@ -1581,28 +1638,6 @@
 
   // Subsequently, we combine all into a single list (FragmentList)
-
-  *out << Verbose(0) << "Combining the lists of all orders per order and finally into a single one." << endl;
-  if (FragmentList == NULL) {
-    FragmentList = new Graph;
-    counter = 0;
-  } else {
-    counter = FragmentList->size();
-  }
-  RootNr = 0;
-  while (!RootStack.empty()) {
-    RootKeyNr = RootStack.front();
-    RootStack.pop_front();
-    Walker = FindAtom(RootKeyNr);
-    NumLevels = 1 << (Walker->AdaptiveOrder - 1);
-    for(int i=0;i<NumLevels;i++) {
-      if (FragmentLowerOrdersList[RootNr][i] != NULL) {
-        InsertGraphIntoGraph(out, *FragmentList, (*FragmentLowerOrdersList[RootNr][i]), &counter);
-        delete(FragmentLowerOrdersList[RootNr][i]);
-      }
-    }
-    Free(&FragmentLowerOrdersList[RootNr]);
-    RootNr++;
-  }
-  Free(&FragmentLowerOrdersList);
+  CombineAllOrderListIntoOne(out, FragmentList, FragmentLowerOrdersList, RootStack, this);
+  FreeAllOrdersList(out, FragmentLowerOrdersList, RootStack, this);
   Free(&NumMoleculesOfOrder);
 
