Index: src/molecules.cpp
===================================================================
--- src/molecules.cpp	(revision d2a2942f50d4672158abc620f6af83d3524d381b)
+++ src/molecules.cpp	(revision df2fcae341603b39aa6e7c81927780a4b56e6472)
@@ -1056,4 +1056,27 @@
     return true;
   }
+};
+
+/** Outputs contents of molecule::ListOfBondsPerAtom.
+ * \param *out output stream
+ */
+void molecule::OutputListOfBonds(ofstream *out) const
+{
+  *out << Verbose(2) << endl << "From Contents of ListOfBondsPerAtom, all non-hydrogen atoms:" << endl;
+  atom *Walker = start;
+  while (Walker->next != end) {
+    Walker = Walker->next; 
+#ifdef ADDHYDROGEN
+    if (Walker->type->Z != 1) {   // regard only non-hydrogen
+#endif
+      *out << Verbose(2) << "Atom " << Walker->Name << " has Bonds: "<<endl;
+      for(int j=0;j<NumberOfBondsPerAtom[Walker->nr];j++) {
+        *out << Verbose(3) << *(ListOfBondsPerAtom)[Walker->nr][j] << endl;
+      }
+#ifdef ADDHYDROGEN
+    }
+#endif
+  }
+  *out << endl;
 };
 
@@ -1940,4 +1963,5 @@
   }
 
+  // free memory
   Free((void **)&filename, "molecule::ParseKeySetFile - filename");
 
@@ -2131,9 +2155,8 @@
           FragmentationToDo = true;
     }
+    if (!FragmentationToDo)
+      *out << Verbose(1) << "Order at every site is already equal or above desired order " << Order << "." << endl;
   } else
     *out << Verbose(1) << "Parsing order at site file failed" << endl;
-
-  if (!FragmentationToDo)
-    *out << Verbose(0) << "Order at every site is already equal or above desired order " << Order << "." << endl;
 };
 
@@ -2169,9 +2192,7 @@
   bool FragmentationToDo = true;
   Graph **FragmentList = NULL;
-  Graph *TempFragmentList = NULL;
+  Graph *ParsedFragmentList = NULL;
   Graph TotalGraph;     // graph with all keysets however local numbers
-  KeySet *TempSet = NULL;
   int TotalNumberOfKeySets = 0;
-  int KeySetCounter = 0;
   atom **ListOfAtoms = NULL;
   atom ***ListOfLocalAtoms = NULL;
@@ -2199,12 +2220,10 @@
 
   // ===== 2. perform a DFS analysis to gather info on cyclic structure and a list of disconnected subgraphs =====
-  Subgraphs = DepthFirstSearchAnalysis((ofstream *)&*out, false, MinimumRingSize);
-  
+  Subgraphs = DepthFirstSearchAnalysis(out, false, MinimumRingSize);
   // fill the bond structure of the individually stored subgraphs
-  FragmentationToDo = FragmentationToDo && Subgraphs->next->FillBondStructureFromReference(out, this, FragmentCounter, ListOfLocalAtoms, false);  // we want to keep the created ListOfLocalAtoms
+  Subgraphs->next->FillBondStructureFromReference(out, this, (FragmentCounter = 0), ListOfLocalAtoms, false);  // we want to keep the created ListOfLocalAtoms
 
   // ===== 3. if structure still valid, parse key set file and others =====
-  TempFragmentList = new Graph;
-  FragmentationToDo = FragmentationToDo && ParseKeySetFile(out, configuration->configpath, TempFragmentList, configuration->GetIsAngstroem());
+  FragmentationToDo = FragmentationToDo && ParseKeySetFile(out, configuration->configpath, ParsedFragmentList, configuration->GetIsAngstroem());
 
   // ===== 4. check globally whether there's something to do actually (first adaptivity check)
@@ -2217,6 +2236,4 @@
     *out << Verbose(0) << "Bond order " << Order << " greater than or equal to Minimum Ring size of " << MinimumRingSize << " found is not allowed." << endl;
   } else {
-    FragmentList = (Graph **) Malloc(sizeof(Graph *)*Subgraphs->next->Count(), "molecule::FragmentMolecule - **BondFragments");
-    
     // ===== 6a. fill RootStack for each subgraph (second adaptivity check) ===== 
     // NOTE: (keep this extern of following while loop, as lateron we may here look for which site to add to which subgraph)
@@ -2226,68 +2243,19 @@
 
    // ===== 6b. assign each keyset to its respective subgraph ===== 
-    if ((TempFragmentList != NULL) && (TempFragmentList->size() != 0)) { // if there are some scanned keysets at all
-      // spread the keysets
-      FragmentCounter = 0;
-      MolecularWalker = Subgraphs;
-      while (MolecularWalker->next != NULL) {
-        MolecularWalker = MolecularWalker->next;
-        // assign scanned keysets
-        FragmentList[FragmentCounter] = new Graph;
-        TempSet = new KeySet;
-        KeySetCounter = 0;
-        for(Graph::iterator runner = TempFragmentList->begin();runner != TempFragmentList->end(); runner++) { // key sets contain global numbers!
-          if ( ListOfLocalAtoms[FragmentCounter][FindAtom(*((*runner).first.begin()))->nr]->nr != -1) {// as we may assume that that bond structure is unchanged, we only test the first key in each set
-            // translate keyset to local numbers
-            for(KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
-              TempSet->insert(ListOfLocalAtoms[FragmentCounter][FindAtom(*sprinter)->nr]->nr);
-            // insert into FragmentList
-            FragmentList[FragmentCounter]->insert(GraphPair (*TempSet, pair<int,double>(KeySetCounter++, (*runner).second.second)));
-          }
-          TempSet->clear();
-        }
-        delete(TempSet);
-        if (KeySetCounter == 0)
-          delete(FragmentList[FragmentCounter]);
-        else
-          *out << Verbose(1) << KeySetCounter << " keysets were assigned to subgraph " << FragmentCounter << "." << endl;
-        FragmentCounter++;
-      }
-    } else  // otherwise make sure all lists are initialised to NULL
-      for(int i=0;i<FragmentCounter;i++)
-        FragmentList[i] = NULL;
-    
+    Subgraphs->next->AssignKeySetsToFragment(out, this, ParsedFragmentList, ListOfLocalAtoms, FragmentList, (FragmentCounter = 0), false);
 
    // ===== 7. fill the bond fragment list =====
     FragmentCounter = 0;
-    TotalNumberOfKeySets = 0;
     MolecularWalker = Subgraphs;
     while (MolecularWalker->next != NULL) {
       MolecularWalker = MolecularWalker->next;
       *out << Verbose(1) << "Fragmenting subgraph " << MolecularWalker << "." << endl;
+      // output ListOfBondsPerAtom for debugging
+      MolecularWalker->Leaf->OutputListOfBonds(out);
       if (MolecularWalker->Leaf->first->next != MolecularWalker->Leaf->last) {
-          // output ListOfBondsPerAtom for debugging
-        *out << Verbose(0) << endl << "From Contents of ListOfBondsPerAtom, all non-hydrogen atoms:" << endl;
-        Walker = MolecularWalker->Leaf->start;
-        while (Walker->next != MolecularWalker->Leaf->end) {
-          Walker = Walker->next; 
-#ifdef ADDHYDROGEN
-          if (Walker->type->Z != 1) {   // regard only non-hydrogen
-#endif
-            *out << Verbose(0) << "Atom " << Walker->Name << " has Bonds: "<<endl;
-            for(int j=0;j<MolecularWalker->Leaf->NumberOfBondsPerAtom[Walker->nr];j++) {
-              *out << Verbose(1) << *(MolecularWalker->Leaf->ListOfBondsPerAtom)[Walker->nr][j] << endl;
-            }
-#ifdef ADDHYDROGEN
-          }
-#endif
-        }
-        *out << endl;
       
+        // call BOSSANOVA method
         *out << Verbose(0) << endl << " ========== BOND ENERGY of subgraph " << FragmentCounter << " ========================= " << endl;
-        *out << Verbose(0) << "Begin of bond fragmentation." << endl;
-        
-        // call BOSSANOVA method
         MolecularWalker->Leaf->FragmentBOSSANOVA(out, FragmentList[FragmentCounter], RootStack[FragmentCounter]);
-        
       } else {
         cerr << "Subgraph " << MolecularWalker << " has no atoms!" << endl;
@@ -2296,8 +2264,13 @@
     }
   }
+  
+  // free the index lookup list
+  for (int i=0;i<FragmentCounter;i++)
+    Free((void **)&ListOfLocalAtoms[i], "molecule::FragmentMolecule - *ListOfLocalAtoms[]");
+  Free((void **)&ListOfLocalAtoms, "molecule::FragmentMolecule - **ListOfLocalAtoms");
 
   // ==================================== End of FRAGMENTATION ============================================
 
-  // ===== 8a. translate list into global numbers (i.e. valid in "this" molecule, not in MolecularWalker->Leaf)
+  // ===== 8a. translate list into global numbers (i.e. ones that are valid in "this" molecule, not in MolecularWalker->Leaf)
   MolecularWalker = Subgraphs;
   FragmentCounter = 0;
@@ -2316,4 +2289,13 @@
   }
   Free((void **)&FragmentList, "molecule::FragmentMolecule - **FragmentList");
+  // free subgraph memory again
+  delete(ParsedFragmentList);
+  if (Subgraphs != NULL) {
+    while (Subgraphs->next != NULL) {
+      Subgraphs = Subgraphs->next;
+      delete(Subgraphs->previous);
+    }
+    delete(Subgraphs);
+  }
 
   // ===== 8b. gather keyset lists (graphs) from all subgraphs and transform into MoleculeListClass =====
@@ -2329,5 +2311,5 @@
   *out << k << "/" << BondFragments->NumberOfMolecules << " fragments generated from the keysets." << endl;
   
-  // ===== 10. Save fragments' configuration and keyset files et al to disk ===
+  // ===== 9. Save fragments' configuration and keyset files et al to disk ===
   if (BondFragments->NumberOfMolecules != 0) {
     // create a SortIndex to map from BFS labels to the sequence in which the atoms are given in the config file
@@ -2377,18 +2359,4 @@
     *out << Verbose(1) << "FragmentList is zero on return, splitting failed." << endl;
   
-  // free the index lookup list
-  for (int i=0;i<FragmentCounter;i++)
-    Free((void **)&ListOfLocalAtoms[i], "molecule::FragmentMolecule - *ListOfLocalAtoms[]");
-  Free((void **)&ListOfLocalAtoms, "molecule::FragmentMolecule - **ListOfLocalAtoms");
-  // free subgraph memory again
-  delete(TempFragmentList);
-  if (Subgraphs != NULL) {
-    while (Subgraphs->next != NULL) {
-      Subgraphs = Subgraphs->next;
-      delete(Subgraphs->previous);
-    }
-    delete(Subgraphs);
-  }
-
   *out << Verbose(0) << "End of bond fragmentation." << endl;
 };
