Index: src/molecules.cpp
===================================================================
--- src/molecules.cpp	(revision 183f35941c82e11c9c474c42c5bccbe92689a8d1)
+++ src/molecules.cpp	(revision b0a0c3848d8b7c7dd67aad39324d6c2a6b8f507a)
@@ -1876,5 +1876,5 @@
 /** Parses the KeySet file and fills \a *FragmentList from the known molecule structure.
  * \param *out output stream for debugging
- * \param *filename name of KeySet file
+ * \param *path path to file
  * \param **ListOfAtoms allocated (molecule::AtomCount) and filled lookup table for ids (Atom::nr) to *Atom
  * \param *FragmentList NULL, filled on return
@@ -1882,9 +1882,10 @@
  * \return true - parsing successfully, false - failure on parsing (FragmentList will be NULL)
  */
-bool molecule::ParseKeySetFile(ofstream *out, char *filename, atom **ListOfAtoms, MoleculeListClass *&FragmentList, bool IsAngstroem)
+bool molecule::ParseKeySetFile(ofstream *out, char *path, atom **ListOfAtoms, MoleculeListClass *&FragmentList, bool IsAngstroem)
 {
   bool status = true;
   ifstream KeySetFile;
   stringstream line;
+  char *filename = (char *) Malloc(sizeof(char)*255, "molecule::ParseKeySetFile - filename");
   
   if (FragmentList != NULL) { // check list pointer
@@ -1894,4 +1895,5 @@
   cout << Verbose(1) << "Parsing the KeySet file ... ";
   // open file and read
+  sprintf(filename, "%s/%s%s", path, "BondFragment", "KeySets.dat");
   KeySetFile.open(filename);
   if (KeySetFile != NULL) {
@@ -1924,4 +1926,113 @@
     status = false;
   }
+  Free((void **)&filename, "molecule::ParseKeySetFile - filename");
+
+  return status;
+};
+
+/** Storing the bond structure of a molecule to file.
+ * Simply stores Atom::nr and then the Atom::nr of all bond partners per line.
+ * \param *out output stream for debugging
+ * \param *path path to file
+ * \return true - file written successfully, false - writing failed
+ */
+bool molecule::StoreAdjacencyToFile(ofstream *out, char *path)
+{
+  ofstream AdjacencyFile;
+  atom *Walker = NULL;
+  char *filename = (char *) Malloc(sizeof(char)*255, "molecule::StoreAdjacencyToFile - filename");
+  bool status = true;
+
+  sprintf(filename, "%s/%s%s", path, "BondFragment", "Adjacency.dat");
+  AdjacencyFile.open(filename);
+  cout << Verbose(1) << "Saving adjacency list ... ";
+  if (AdjacencyFile != NULL) {
+    Walker = start;
+    while(Walker->next != end) {
+      Walker = Walker->next;
+      AdjacencyFile << Walker->nr << "\t";
+      for (int i=0;i<NumberOfBondsPerAtom[Walker->nr];i++)
+        AdjacencyFile << ListOfBondsPerAtom[Walker->nr][i]->GetOtherAtom(Walker)->nr << "\t";
+      AdjacencyFile << endl;
+    }
+    AdjacencyFile.close();
+    cout << "done." << endl;
+  } else {
+    cout << "failed." << endl;
+    status = false;
+  }
+  Free((void **)&filename, "molecule::StoreAdjacencyToFile - filename");
+  
+  return status;
+};
+
+/** Checks contents of adjacency file against bond structure in structure molecule.
+ * \param *out output stream for debugging
+ * \param *path path to file
+ * \param **ListOfAtoms allocated (molecule::AtomCount) and filled lookup table for ids (Atom::nr) to *Atom
+ * \return true - structure is equal, false - not equivalence
+ */ 
+bool molecule::CheckAdjacencyFileAgainstMolecule(ofstream *out, char *path, atom **ListOfAtoms)
+{
+  char *filename = (char *) Malloc(sizeof(char)*255, "molecule::CheckAdjacencyFileAgainstMolecule - filename");
+  ifstream File;
+  bool status = true;
+  
+  sprintf(filename, "%s/%s%s", path, "BondFragment", "Adjacency.dat");
+  File.open(filename);
+  *out << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ...";
+  if (File != NULL) {
+    // allocate storage structure
+    int NonMatchNumber = 0;   // will number of atoms with differing bond structure
+    int *CurrentBonds = (int *) Malloc(sizeof(int)*8, "molecule::CheckAdjacencyFileAgainstMolecule - CurrentBonds"); // contains parsed bonds of current atom
+    int CurrentBondsOfAtom;
+    // Parse the file line by line and count the bonds
+    while (!File.eof()) {
+      File.getline(filename, 255);
+      stringstream line;
+      line.str(filename);
+      int AtomNr = -1;
+      line >> AtomNr;
+      CurrentBondsOfAtom = -1; // we count one too far due to line end
+      // parse into structure
+      if ((AtomNr >= 0) && (AtomNr < AtomCount)) {
+        while (!line.eof())
+          line >> CurrentBonds[ ++CurrentBondsOfAtom ];
+        // compare against present bonds
+        //cout << Verbose(2) << "Walker is " << *Walker << ", bond partners: ";
+        if (CurrentBondsOfAtom == NumberOfBondsPerAtom[AtomNr]) {
+          for(int i=0;i<NumberOfBondsPerAtom[AtomNr];i++) {
+            int id = ListOfBondsPerAtom[AtomNr][i]->GetOtherAtom(ListOfAtoms[AtomNr])->nr;
+            int j = 0;
+            for (;(j<CurrentBondsOfAtom) && (CurrentBonds[j++] != id);); // check against all parsed bonds
+            if (CurrentBonds[j-1] != id) { // no match ? Then mark in ListOfAtoms
+              ListOfAtoms[AtomNr] = NULL;
+              NonMatchNumber++;
+              status = false;
+              //out << "[" << id << "]\t";
+            } else {
+              //out << id << "\t";
+            }
+          }
+          //out << endl;
+        } else {
+          *out << "Number of bonds for Atom " << *ListOfAtoms[AtomNr] << " does not match, parsed " << CurrentBondsOfAtom << " against " << NumberOfBondsPerAtom[AtomNr] << "." << endl;
+          status = false;
+        }
+      }
+    }
+    File.close();
+    File.clear();
+    if (status) { // if equal we parse the KeySetFile
+      *out << " done: Equal." << endl;
+      status = true;
+    } else
+      *out << " done: Not equal by " << NonMatchNumber << " atoms." << endl;
+    Free((void **)&CurrentBonds, "molecule::CheckAdjacencyFileAgainstMolecule - **CurrentBonds");
+  } else {
+    *out << " Adjacency file not found." << endl;
+    status = false;
+  }
+  Free((void **)&filename, "molecule::CheckAdjacencyFileAgainstMolecule - filename");
   
   return status;
@@ -1951,5 +2062,4 @@
   MoleculeLeafClass *Subgraphs = NULL;      // list of subgraphs from DFS analysis
   fstream File;
-  char *filename = NULL;
   bool status = true;
 
@@ -1964,101 +2074,28 @@
   CreateListOfBondsPerAtom(out);
 
-  // === open and compare adjacency file ===
-  filename = (char *) Malloc(sizeof(char)*255, "molecule::FragmentMolecule - filename");
-  sprintf(filename, "%s/%s%s", configuration->GetDefaultPath(), "BondFragment", "Adjacency.dat");
-  File.open(filename, ios::in);
-  cout << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ...";
-  if (File != NULL) {
-    // allocate storage structure
-    atom **ListOfAtoms = (atom **) Malloc(sizeof(atom *)*AtomCount, "molecule::FragmentMolecule - ListOfAtoms"); // will contain the atom ids whose bond partners differ
-    int NonMatchNumber = 0;   // will number of atoms with differing bond structure
-    int *CurrentBonds = (int *) Malloc(sizeof(int)*8, "molecule::FragmentMolecule - CurrentBonds"); // contains parsed bonds of current atom
-    int CurrentBondsOfAtom;
-    Walker = start;
-    while (Walker->next != end) {
-      Walker = Walker->next;
-      if ((Walker->nr >= 0) && (Walker->nr < AtomCount)) {
-        ListOfAtoms[Walker->nr] = Walker;
-      } else
-        break;
-    }
-    if (Walker->next == end) {  // everything went alright
-      // Parse the file line by line and count the bonds
-      while (!File.eof()) {
-        File.getline(filename, 255);
-        stringstream line;
-        line.str(filename);
-        int AtomNr = -1;
-        line >> AtomNr;
-        CurrentBondsOfAtom = -1; // we count one too far due to line end
-        // parse into structure
-        if ((AtomNr >= 0) && (AtomNr < AtomCount)) {
-          while (!line.eof())
-            line >> CurrentBonds[ ++CurrentBondsOfAtom ];
-          // compare against present bonds
-          Walker = ListOfAtoms[AtomNr];
-          //cout << Verbose(2) << "Walker is " << *Walker << ", bond partners: ";
-          if (CurrentBondsOfAtom == NumberOfBondsPerAtom[AtomNr]) {
-            for(int i=0;i<NumberOfBondsPerAtom[AtomNr];i++) {
-              int id = ListOfBondsPerAtom[AtomNr][i]->GetOtherAtom(Walker)->nr;
-              int j = 0;
-              for (;(j<CurrentBondsOfAtom) && (CurrentBonds[j++] != id);); // check against all parsed bonds
-              if (CurrentBonds[j-1] != id) { // no match ? Then mark in ListOfAtoms
-                ListOfAtoms[AtomNr] = NULL;
-                NonMatchNumber++;
-                status = false;
-                //cout << "[" << id << "]\t";
-              } else {
-                //cout << id << "\t";
-              }
-            }
-            //cout << endl;
-          } else {
-            cout << "Number of bonds for Atom " << *Walker << " does not match, parsed " << CurrentBondsOfAtom << " against " << NumberOfBondsPerAtom[AtomNr] << "." << endl;
-            status = false;
-          }
-        }
-      }
-      File.close();
-      File.clear();
-      if (status) { // if equal we parse the KeySetFile
-        cout << " done: Equal." << endl;
-        sprintf(filename, "%s/%s%s", configuration->GetDefaultPath(), "BondFragment", "KeySets.dat");
-        status = ParseKeySetFile(out, filename, ListOfAtoms, FragmentList, configuration->GetIsAngstroem());
-      } else
-        cout << " done: Not equal by " << NonMatchNumber << " atoms." << endl;
-    } else {
-      cout << " range of nuclear ids exceeded [0, AtomCount)." << endl;
-      status = false;
-    }
-    Free((void **)&CurrentBonds, "molecule::FragmentMolecule - **CurrentBonds");
-    Free((void **)&ListOfAtoms, "molecule::FragmentMolecule - *ListOfAtoms");
-  } else {
-    cout << " Adjacency file not found." << endl;
+  // === compare it with adjacency file ===
+  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;
     status = false;
   }
+  if (CheckAdjacencyFileAgainstMolecule(out, configuration->GetDefaultPath(), ListOfAtoms)) {  // NULL entries in ListOfAtoms contain NonMatches
+    status = ParseKeySetFile(out, configuration->GetDefaultPath(), ListOfAtoms, FragmentList, configuration->GetIsAngstroem());
+  }
+  Free((void **)&ListOfAtoms, "molecule::FragmentMolecule - **ListOfAtoms");
 
   // =================================== Begin of FRAGMENTATION ===============================
   if (!status) {
     // === store Adjacency file ===
-    sprintf(filename, "%s/%s%s", configuration->GetDefaultPath(), "BondFragment", "Adjacency.dat");
-    File.open(filename, ios::out);
-    cout << Verbose(1) << "Saving adjacency list ... ";
-    if (File != NULL) {
-      Walker = start;
-      while(Walker->next != end) {
-        Walker = Walker->next;
-        File << Walker->nr << "\t";
-        for (int i=0;i<NumberOfBondsPerAtom[Walker->nr];i++)
-          File << ListOfBondsPerAtom[Walker->nr][i]->GetOtherAtom(Walker)->nr << "\t";
-        File << endl;
-      }
-      File.close();
-      cout << "done." << endl;
-    } else {
-      cout << "failed." << endl;
-    }
-    Free((void **)&filename, "molecule::FragmentMolecule - filename");
-  
+    StoreAdjacencyToFile(out, configuration->GetDefaultPath());
+
     // === first perform a DFS analysis to gather info on cyclic structure and a list of disconnected subgraphs ===
     Subgraphs = DepthFirstSearchAnalysis((ofstream *)&cout, false, MinimumRingSize);
