Index: src/moleculelist.cpp
===================================================================
--- src/moleculelist.cpp	(revision 87f6c98765e07307ce97e48dcfbb5a4069cdcfac)
+++ src/moleculelist.cpp	(revision bf46daa8afbb82c7de05fbedc890325cd98c83eb)
@@ -503,5 +503,5 @@
   if (FragmentList == NULL) {
     KeySetCounter = Count();
-    FragmentList = (Graph **) Malloc(sizeof(Graph *)*KeySetCounter, "molecule::FragmentMolecule - **BondFragments");
+    FragmentList = (Graph **) Malloc(sizeof(Graph *)*KeySetCounter, "MoleculeLeafClass::AssignKeySetsToFragment - **FragmentList");
     for(int i=0;i<KeySetCounter;i++)
       FragmentList[i] = NULL;
Index: src/molecules.cpp
===================================================================
--- src/molecules.cpp	(revision 87f6c98765e07307ce97e48dcfbb5a4069cdcfac)
+++ src/molecules.cpp	(revision bf46daa8afbb82c7de05fbedc890325cd98c83eb)
@@ -2161,4 +2161,36 @@
 };
 
+/** Create a SortIndex to map from BFS labels to the sequence in which the atoms are given in the config file.
+ * \param *out output stream for debugging
+ * \param *&SortIndex Mapping array of size molecule::AtomCount
+ * \return true - success, false - failure of SortIndex alloc
+ */
+bool molecule::CreateMappingLabelsToConfigSequence(ofstream *out, int *&SortIndex)
+{
+  element *runner = elemente->start;
+  int AtomNo = 0;
+  atom *Walker = NULL;
+  
+  if (SortIndex != NULL) {
+    *out << Verbose(1) << "SortIndex is " << SortIndex << " and not NULL as expected." << endl;
+    return false;
+  }
+  SortIndex = (int *) Malloc(sizeof(int)*AtomCount, "molecule::FragmentMolecule: *SortIndex");
+  for(int i=0;i<AtomCount;i++)
+    SortIndex[i] = -1;
+  while (runner->next != elemente->end) { // go through every element
+    runner = runner->next;
+    if (ElementsInMolecule[runner->Z]) { // if this element got atoms
+      Walker = start;
+      while (Walker->next != end) { // go through every atom of this element
+        Walker = Walker->next;
+        if (Walker->type->Z == runner->Z) // if this atom fits to element
+          SortIndex[Walker->nr] = AtomNo++;
+      }
+    }
+  }
+  return true;
+};
+
 /** Performs a many-body bond order analysis for a given bond order.
  * -# parses adjacency, keysets and orderatsite files
@@ -2181,8 +2213,5 @@
 {
 	MoleculeListClass *BondFragments = NULL;
-  atom *Walker = NULL;
   int *SortIndex = NULL;
-  element *runner = NULL;
-  int AtomNo;
   int MinimumRingSize;
   int FragmentCounter;
@@ -2302,21 +2331,7 @@
   // ===== 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
-    SortIndex = (int *) Malloc(sizeof(int)*AtomCount, "molecule::FragmentMolecule: *SortIndex");
-    for(int i=0;i<AtomCount;i++)
-      SortIndex[i] = -1;
-    runner = elemente->start;
-    AtomNo = 0;
-    while (runner->next != elemente->end) { // go through every element
-      runner = runner->next;
-      if (ElementsInMolecule[runner->Z]) { // if this element got atoms
-        Walker = start;
-        while (Walker->next != end) { // go through every atom of this element
-          Walker = Walker->next;
-          if (Walker->type->Z == runner->Z) // if this atom fits to element
-            SortIndex[Walker->nr] = AtomNo++;
-        }
-      }
-    }
+    // create the SortIndex from BFS labels to order in the config file
+    CreateMappingLabelsToConfigSequence(out, SortIndex);
+    
     *out << Verbose(1) << "Writing " << BondFragments->NumberOfMolecules << " possible bond fragmentation configs" << endl;
     if (BondFragments->OutputConfigForListOfFragments(out, configuration, SortIndex))
@@ -2343,5 +2358,4 @@
     *out << Verbose(1) << "Freeing bond memory" << endl;
     delete(FragmentList); // remove bond molecule from memory
-    FragmentList = NULL;
     Free((void **)&SortIndex, "molecule::FragmentMolecule: *SortIndex"); 
   } else
Index: src/molecules.hpp
===================================================================
--- src/molecules.hpp	(revision 87f6c98765e07307ce97e48dcfbb5a4069cdcfac)
+++ src/molecules.hpp	(revision bf46daa8afbb82c7de05fbedc890325cd98c83eb)
@@ -348,4 +348,5 @@
   bool StoreKeySetFile(ofstream *out, Graph &KeySetList, char *path);
   bool StoreForcesFile(ofstream *out, MoleculeListClass *BondFragments, char *path, int *SortIndex);
+  bool CreateMappingLabelsToConfigSequence(ofstream *out, int *&SortIndex);
   bool ScanBufferIntoKeySet(ofstream *out, char *buffer, KeySet &CurrentSet);
   void BreadthFirstSearchAdd(ofstream *out, molecule *Mol, atom **&AddedAtomList, bond **&AddedBondList, atom *Root, bond *Bond, int BondOrder, bool IsAngstroem);
