Index: src/molecule_graph.cpp
===================================================================
--- src/molecule_graph.cpp	(revision df8b199baa82d9d524d867256b1f58ec88ba477c)
+++ src/molecule_graph.cpp	(revision ba4170d2dfbebfe434b06d01aed19c832651e2d6)
@@ -846,4 +846,53 @@
 };
 
+bool CheckAdjacencyFileAgainstMolecule_Init(ofstream *out, char *path, ifstream &File, int *&CurrentBonds)
+{
+  stringstream filename;
+  filename << path << "/" << FRAGMENTPREFIX << ADJACENCYFILE;
+  File.open(filename.str().c_str(), ios::out);
+  *out << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... ";
+  if (File == NULL)
+    return false;
+
+  // allocate storage structure
+  CurrentBonds = Malloc<int>(8, "molecule::CheckAdjacencyFileAgainstMolecule - CurrentBonds"); // contains parsed bonds of current atom
+  return true;
+};
+
+void CheckAdjacencyFileAgainstMolecule_Finalize(ofstream *out, ifstream &File, int *&CurrentBonds)
+{
+  File.close();
+  File.clear();
+  Free(&CurrentBonds);
+};
+
+void CheckAdjacencyFileAgainstMolecule_CompareBonds(ofstream *out, bool &status, int &NonMatchNumber, atom *&Walker, size_t &CurrentBondsOfAtom, int AtomNr, int *&CurrentBonds, atom **ListOfAtoms)
+{
+  size_t j = 0;
+  int id = -1;
+
+  //*out << Verbose(2) << "Walker is " << *Walker << ", bond partners: ";
+  if (CurrentBondsOfAtom == Walker->ListOfBonds.size()) {
+    for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
+      id = (*Runner)->GetOtherAtom(Walker)->nr;
+      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 " << *Walker << " does not match, parsed " << CurrentBondsOfAtom << " against " << Walker->ListOfBonds.size() << "." << endl;
+    status = false;
+  }
+};
+
 /** Checks contents of adjacency file against bond structure in structure molecule.
  * \param *out output stream for debugging
@@ -855,69 +904,41 @@
 {
   ifstream File;
-  stringstream filename;
   bool status = true;
   atom *Walker = NULL;
-  char *buffer = Malloc<char>(MAXSTRINGSIZE, "molecule::CheckAdjacencyFileAgainstMolecule: *buffer");
-
-  filename << path << "/" << FRAGMENTPREFIX << ADJACENCYFILE;
-  File.open(filename.str().c_str(), ios::out);
-  *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 = Malloc<int>(8, "molecule::CheckAdjacencyFileAgainstMolecule - CurrentBonds"); // contains parsed bonds of current atom
-    size_t CurrentBondsOfAtom;
-
-    // Parse the file line by line and count the bonds
-    while (!File.eof()) {
-      File.getline(buffer, MAXSTRINGSIZE);
-      stringstream line;
-      line.str(buffer);
-      int AtomNr = -1;
-      line >> AtomNr;
-      CurrentBondsOfAtom = -1; // we count one too far due to line end
-      // parse into structure
-      if ((AtomNr >= 0) && (AtomNr < AtomCount)) {
-        Walker = ListOfAtoms[AtomNr];
-        while (!line.eof())
-          line >> CurrentBonds[ ++CurrentBondsOfAtom ];
-        // compare against present bonds
-        //cout << Verbose(2) << "Walker is " << *Walker << ", bond partners: ";
-        if (CurrentBondsOfAtom == Walker->ListOfBonds.size()) {
-          for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
-            int id = (*Runner)->GetOtherAtom(Walker)->nr;
-            size_t 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 " << *Walker << " does not match, parsed " << CurrentBondsOfAtom << " against " << Walker->ListOfBonds.size() << "." << endl;
-          status = false;
-        }
-      }
-    }
-    File.close();
-    File.clear();
-    if (status) { // if equal we parse the KeySetFile
-      *out << Verbose(1) << "done: Equal." << endl;
-      status = true;
-    } else
-      *out << Verbose(1) << "done: Not equal by " << NonMatchNumber << " atoms." << endl;
-    Free(&CurrentBonds);
-  } else {
+  char *buffer = NULL;
+  int *CurrentBonds = NULL;
+  int NonMatchNumber = 0;   // will number of atoms with differing bond structure
+  size_t CurrentBondsOfAtom = -1;
+
+  if (!CheckAdjacencyFileAgainstMolecule_Init(out, path, File, CurrentBonds)) {
     *out << Verbose(1) << "Adjacency file not found." << endl;
-    status = false;
-  }
-  *out << endl;
+    return true;
+  }
+
+  buffer = Malloc<char>(MAXSTRINGSIZE, "molecule::CheckAdjacencyFileAgainstMolecule: *buffer");
+  // Parse the file line by line and count the bonds
+  while (!File.eof()) {
+    File.getline(buffer, MAXSTRINGSIZE);
+    stringstream line;
+    line.str(buffer);
+    int AtomNr = -1;
+    line >> AtomNr;
+    CurrentBondsOfAtom = -1; // we count one too far due to line end
+    // parse into structure
+    if ((AtomNr >= 0) && (AtomNr < AtomCount)) {
+      Walker = ListOfAtoms[AtomNr];
+      while (!line.eof())
+        line >> CurrentBonds[ ++CurrentBondsOfAtom ];
+      // compare against present bonds
+      CheckAdjacencyFileAgainstMolecule_CompareBonds(out, status, NonMatchNumber, Walker, CurrentBondsOfAtom, AtomNr, CurrentBonds, ListOfAtoms);
+    }
+  }
   Free(&buffer);
-
+  CheckAdjacencyFileAgainstMolecule_Finalize(out, File, CurrentBonds);
+
+  if (status) { // if equal we parse the KeySetFile
+    *out << Verbose(1) << "done: Equal." << endl;
+  } else
+    *out << Verbose(1) << "done: Not equal by " << NonMatchNumber << " atoms." << endl;
   return status;
 };
