Index: src/molecules.cpp
===================================================================
--- src/molecules.cpp	(revision c82f3d1d590c3a59b332f8f562e3ffddb0d32ec3)
+++ src/molecules.cpp	(revision 9fcf479595dd39cf6703677eb220251fac77a2e7)
@@ -2111,4 +2111,31 @@
 };
 
+/** Checks whether the OrderAtSite is still bewloe \a Order at some site.
+ * \param *out output stream for debugging
+ * \param &FragmentationToDo return boolean
+ * \param Order desired Order
+ */
+void molecule::CheckOrderAtSite(ofstream *out, bool &FragmentationToDo, int Order)
+{
+  atom *Walker = NULL; 
+  
+  if (FragmentationToDo) { // check whether OrderAtSite is above Order everywhere
+    FragmentationToDo = false;
+    Walker = start;
+    while (Walker->next != end) {
+      Walker = Walker->next;
+#ifdef ADDHYDROGEN
+      if (Walker->type->Z != 1) // skip hydrogen
+#endif
+        if (Walker->AdaptiveOrder < Order)
+          FragmentationToDo = true;
+    }
+  } 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;
+};
+
 /** Performs a many-body bond order analysis for a given bond order.
  * -# parses adjacency, keysets and orderatsite files
@@ -2132,6 +2159,4 @@
 	MoleculeListClass *BondFragments = NULL;
   atom *Walker = NULL;
-  atom *OtherWalker = NULL;
-  bond *Binder = NULL;
   int *SortIndex = NULL;
   element *runner = NULL;
@@ -2149,4 +2174,5 @@
   int TotalNumberOfKeySets = 0;
   int KeySetCounter = 0;
+  atom **ListOfAtoms = NULL;
   atom ***ListOfLocalAtoms = NULL;
   
@@ -2158,4 +2184,6 @@
 #endif
 
+  // ++++++++++++++++++++++++++++ INITIAL STUFF: Bond structure analysis, ... ++++++++++++++++++++++++++++++++++++++++++
+  
   // ===== 1. Check whether bond structure is same as stored in files ====
   
@@ -2164,17 +2192,6 @@
 
   // create lookup table for Atom::nr
-  atom **ListOfAtoms = (atom **) Malloc(sizeof(atom *)*AtomCount, "molecule::FragmentMolecule - **ListOfAtoms");
-  Walker = start;
-  while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron 
-    Walker = Walker->next;
-    if ((Walker->nr >= 0) && (Walker->nr < AtomCount)) {
-      ListOfAtoms[Walker->nr] = Walker;
-    } else
-      break;
-  }
-  if (Walker->next != end) {  // everything went alright
-    *out << " range of nuclear ids exceeded [0, AtomCount)." << endl;
-    FragmentationToDo = false;
-  }
+  FragmentationToDo = FragmentationToDo && CreateFatherLookupTable(out, start, end, ListOfAtoms, AtomCount);
+  
   // === compare it with adjacency file ===
   FragmentationToDo = FragmentationToDo && CheckAdjacencyFileAgainstMolecule(out, configuration->configpath, ListOfAtoms); 
@@ -2184,78 +2201,15 @@
   Subgraphs = DepthFirstSearchAnalysis((ofstream *)&*out, false, MinimumRingSize);
   
-  // fill index lookup list for each subgraph from global nr to local pointer
-  ListOfLocalAtoms = (atom ***) Malloc(sizeof(atom **)*FragmentCounter, "molecule::FragmentMolecule - **ListOfLocalAtoms");
-  for (int i=0;i<FragmentCounter;i++) {
-    ListOfLocalAtoms[i] = (atom **) Malloc(sizeof(atom *)*AtomCount, "molecule::FragmentMolecule - *ListOfLocalAtoms[]");
-    for(int j=0;j<AtomCount;j++)
-      ListOfLocalAtoms[i][j] = NULL;
-  }
-  FragmentCounter = 0;
-  MolecularWalker = Subgraphs;
-  while (MolecularWalker->next != NULL) {
-    MolecularWalker = MolecularWalker->next;
-    Walker = MolecularWalker->Leaf->start;
-    while (Walker->next != MolecularWalker->Leaf->end) {
-      Walker = Walker->next;
-#ifdef ADDHYDROGEN
-      if (Walker->type->Z != 1) // skip hydrogen
-#endif
-        ListOfLocalAtoms[FragmentCounter][Walker->GetTrueFather()->nr] = Walker;  // set present global id index to the local pointer
-    }
-    FragmentCounter++;
-  }
-
   // fill the bond structure of the individually stored subgraphs
-  FragmentCounter = 0;
-  MolecularWalker = Subgraphs;
-  while (MolecularWalker->next != NULL) {
-    MolecularWalker = MolecularWalker->next;
-    *out << Verbose(1) << "Creating adjacency list for subgraph " << MolecularWalker << "." << endl;
-    Walker = MolecularWalker->Leaf->start;
-    while (Walker->next != MolecularWalker->Leaf->end) {
-      Walker = Walker->next;
-      AtomNo = Walker->father->nr;  // global id of the current walker
-      for(int i=0;i<NumberOfBondsPerAtom[AtomNo];i++) { // go through father's bonds and copy them all
-        Binder = ListOfBondsPerAtom[AtomNo][i];
-        OtherWalker = ListOfLocalAtoms[FragmentCounter][Binder->GetOtherAtom(Walker->father)->nr];    // local copy of current bond partner of walker
-        if ((OtherWalker != NULL) && (OtherWalker->nr > Walker->nr))
-          MolecularWalker->Leaf->AddBond(Walker, OtherWalker, Binder->BondDegree);
-      }
-    }
-    //MolecularWalker->Leaf->CreateAdjacencyList(out, BondDistance);
-    MolecularWalker->Leaf->CreateListOfBondsPerAtom(out);
-    FragmentCounter++;
-  }
+  FragmentationToDo = FragmentationToDo && Subgraphs->next->FillBondStructureFromReference(out, this, FragmentCounter, ListOfLocalAtoms, false);  // we want to keep the created ListOfLocalAtoms
 
   // ===== 3. if structure still valid, parse key set file and others =====
-  if (FragmentationToDo) {
-    // parse key sets into new graph
-    TempFragmentList = new Graph;
-    FragmentationToDo = FragmentationToDo && ParseKeySetFile(out, configuration->configpath, TempFragmentList, configuration->GetIsAngstroem());
-  } else
-    *out << Verbose(1) << "Creation of lookup table Atom::nr <-> Atom failed!" << endl;
-  if (FragmentationToDo)  // parse the adaptive order per atom/site/vertex
-    FragmentationToDo = FragmentationToDo && ParseOrderAtSiteFromFile(out, configuration->configpath);
-  else
-     *out << Verbose(1) << "Parsing keyset file failed" << endl;
-  
+  TempFragmentList = new Graph;
+  FragmentationToDo = FragmentationToDo && ParseKeySetFile(out, configuration->configpath, TempFragmentList, configuration->GetIsAngstroem());
+
   // ===== 4. check globally whether there's something to do actually (first adaptivity check)
-  if (FragmentationToDo) { // check whether OrderAtSite is above Order everywhere
-    FragmentationToDo = false;
-    Walker = start;
-    while (Walker->next != end) {
-      Walker = Walker->next;
-#ifdef ADDHYDROGEN
-      if (Walker->type->Z != 1) // skip hydrogen
-#endif
-        if (Walker->AdaptiveOrder < Order)
-          FragmentationToDo = true;
-    }
-  } 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;
-
+  FragmentationToDo = FragmentationToDo && ParseOrderAtSiteFromFile(out, configuration->configpath);
+  CheckOrderAtSite(out, FragmentationToDo, Order);
+  
   // =================================== Begin of FRAGMENTATION =============================== 
   // check cyclic lengths
@@ -2263,11 +2217,4 @@
     *out << Verbose(0) << "Bond order " << Order << " greater than or equal to Minimum Ring size of " << MinimumRingSize << " found is not allowed." << endl;
   } else {
-    FragmentCounter = 0;
-    MolecularWalker = Subgraphs;
-    // count subgraphs and allocate fragments
-    while (MolecularWalker->next != NULL) {
-      MolecularWalker = MolecularWalker->next;
-      FragmentCounter++;
-    }
     FragmentList = (Graph **) Malloc(sizeof(Graph *)*FragmentCounter, "molecule::FragmentMolecule - **BondFragments");
     
@@ -2325,5 +2272,5 @@
           delete(FragmentList[FragmentCounter]);
         else
-          *out << Verbose(1) << KeySetCounter << " atoms were put into subgraph " << FragmentCounter << "." << endl;
+          *out << Verbose(1) << KeySetCounter << " keysets were assigned to subgraph " << FragmentCounter << "." << endl;
         FragmentCounter++;
       }
@@ -2365,14 +2312,4 @@
         MolecularWalker->Leaf->FragmentBOSSANOVA(out, FragmentList[FragmentCounter], RootStack[FragmentCounter]);
         
-        // translate list into global numbers (i.e. valid in "this" molecule, not in MolecularWalker->Leaf)
-        KeySet *TempSet = new KeySet;
-        for(Graph::iterator runner = FragmentList[FragmentCounter]->begin(); runner != FragmentList[FragmentCounter]->end(); runner++) {
-          for(KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
-            TempSet->insert((MolecularWalker->Leaf->FindAtom(*sprinter))->GetTrueFather()->nr);
-          TotalGraph.insert(GraphPair(*TempSet, pair<int,double>(TotalNumberOfKeySets++, (*runner).second.second)));
-          TempSet->clear();
-        }
-        delete(TempSet);
-        delete(FragmentList[FragmentCounter]);
       } else {
         cerr << "Subgraph " << MolecularWalker << " has no atoms!" << endl;
@@ -2381,7 +2318,26 @@
     }
   }
+
+  // ==================================== End of FRAGMENTATION ============================================
+
+  // ===== 8a. translate list into global numbers (i.e. valid in "this" molecule, not in MolecularWalker->Leaf)
+  MolecularWalker = Subgraphs;
+  FragmentCounter = 0;
+  while (MolecularWalker->next != NULL) {
+    MolecularWalker = MolecularWalker->next;
+    KeySet *TempSet = new KeySet;
+    for(Graph::iterator runner = FragmentList[FragmentCounter]->begin(); runner != FragmentList[FragmentCounter]->end(); runner++) {
+      for(KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
+        TempSet->insert((MolecularWalker->Leaf->FindAtom(*sprinter))->GetTrueFather()->nr);
+      TotalGraph.insert(GraphPair(*TempSet, pair<int,double>(TotalNumberOfKeySets++, (*runner).second.second)));
+      TempSet->clear();
+    }
+    delete(TempSet);
+    delete(FragmentList[FragmentCounter]);
+    FragmentCounter++;
+  }
   Free((void **)&FragmentList, "molecule::FragmentMolecule - **FragmentList");
 
-  // ===== 8. gather keyset lists (graphs) from all subgraphs and transform into MoleculeListClass =====
+  // ===== 8b. gather keyset lists (graphs) from all subgraphs and transform into MoleculeListClass =====
   // allocate memory for the pointer array and transmorph graphs into full molecular fragments
   BondFragments = new MoleculeListClass(TotalGraph.size(), AtomCount);
@@ -2394,6 +2350,4 @@
   }
   *out << k << "/" << BondFragments->NumberOfMolecules << " fragments generated from the keysets." << endl;
-
- // ==================================== End of FRAGMENTATION ================================
   
   // ===== 10. Save fragments' configuration and keyset files et al to disk ===
Index: src/molecules.hpp
===================================================================
--- src/molecules.hpp	(revision c82f3d1d590c3a59b332f8f562e3ffddb0d32ec3)
+++ src/molecules.hpp	(revision 9fcf479595dd39cf6703677eb220251fac77a2e7)
@@ -340,4 +340,5 @@
   /// Fragment molecule by two different approaches:
   void FragmentMolecule(ofstream *out, int Order, config *configuration);
+  void CheckOrderAtSite(ofstream *out, bool &FragmentationToDo, int Order);
   bool StoreAdjacencyToFile(ofstream *out, char *path);
   bool CheckAdjacencyFileAgainstMolecule(ofstream *out, char *path, atom **ListOfAtoms);
@@ -408,4 +409,5 @@
 
   bool AddLeaf(molecule *ptr, MoleculeLeafClass *Previous);
+  bool FillBondStructureFromReference(ofstream *out, molecule *reference, int &FragmentCounter, atom ***&ListOfLocalAtoms, bool FreeList = false);
 };
 
