Index: src/Actions/FragmentationAction/SubgraphDissectionAction.cpp
===================================================================
--- src/Actions/FragmentationAction/SubgraphDissectionAction.cpp	(revision c449d9b8a0ac9cca5b6e85e320c3732e5c61ccee)
+++ src/Actions/FragmentationAction/SubgraphDissectionAction.cpp	(revision 7d8342bd474d4727096bdd22952cb35fbc6b3e72)
@@ -37,4 +37,5 @@
 
 typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
+typedef std::map< atomId_t, atomId_t > AtomAtomList;
 
 using namespace std;
@@ -52,5 +53,5 @@
   DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
 
-  // first create undo state
+  // first create stuff for undo state
   MolAtomList moleculelist;
   vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
@@ -65,8 +66,6 @@
   FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist, params);
 
+  // 0a. remove all present molecules
   MoleculeListClass *molecules = World::getInstance().getMolecules();
-  config * const configuration = World::getInstance().getConfig();
-
-  // 0a. remove all present molecules
   for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
     molecules->erase(*MolRunner);
@@ -76,9 +75,11 @@
   // 0b. remove all bonds and construct a molecule with all atoms
   molecule *mol = World::getInstance().createMolecule();
+  int BondCount = 0;
   {
     vector <atom *> allatoms = World::getInstance().getAllAtoms();
     for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
-      for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
-        delete(*BondRunner);
+      BondCount += (*AtomRunner)->ListOfBonds.size();
+//      for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
+//        delete(*BondRunner);
       mol->AddAtom(*AtomRunner);
     }
@@ -86,13 +87,16 @@
 
   // 1. create the bond structure of the single molecule
-  if (configuration->BG != NULL) {
-    if (!configuration->BG->ConstructBondGraph(mol)) {
-      World::getInstance().destroyMolecule(mol);
-      DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
+  if (BondCount == 0) {
+    config * const configuration = World::getInstance().getConfig();
+    if ((configuration->BG != NULL)) {
+      if (!configuration->BG->ConstructBondGraph(mol)) {
+        World::getInstance().destroyMolecule(mol);
+        DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
+        return Action::failure;
+      }
+    } else {
+      DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
       return Action::failure;
     }
-  } else {
-    DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
-    return Action::failure;
   }
 
@@ -107,20 +111,46 @@
     return Action::failure;
   }
-  int FragmentCounter = Subgraphs->next->Count();
+
+  //int FragmentCounter = Subgraphs->next->Count();
 
   // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
   {
-    // 3a. copy the bonds
-    atom **ListOfAtoms = NULL;
-    Subgraphs->next->FillBondStructureFromReference(mol, ListOfAtoms, true);  // we want to keep the created ListOfLocalAtoms
-    // 3b. correct fathers (AddCopyAtom sets father to original atom, which has been destroyed).
+    {
+      atom **ListOfAtoms = NULL;
+      // 3a. re-create bond structure and insert molecules into general MoleculeListClass
+      MoleculeLeafClass *MoleculeWalker = Subgraphs->next;
+      while (MoleculeWalker->next != NULL) {
+        ListOfAtoms = NULL;
+        MoleculeWalker->FillBondStructureFromReference(mol, ListOfAtoms, true);  // we want to keep the created ListOfLocalAtoms
+        molecules->insert(MoleculeWalker->Leaf);
+        MoleculeWalker = MoleculeWalker->next;
+      }
+      molecules->insert(MoleculeWalker->Leaf);
+      ListOfAtoms = NULL;
+      MoleculeWalker->FillBondStructureFromReference(mol, ListOfAtoms, true);  // we want to keep the created ListOfLocalAtoms
+    }
+
+    // 3b. store map from new to old ids for 3d
     vector <atom *> allatoms = World::getInstance().getAllAtoms();
-    for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
-      (*AtomRunner)->father = *AtomRunner;
-    }
-    // 3c. destroy the original molecule
-    for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
-      World::getInstance().destroyAtom(*AtomRunner);
-    World::getInstance().destroyMolecule(mol);
+    AtomAtomList newtooldlist;
+    for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner)
+      if ((*AtomRunner)->father != (*AtomRunner))
+        newtooldlist.insert( std::pair<atomId_t, atomId_t> ((*AtomRunner)->getId(),(*AtomRunner)->father->getId()) );
+
+    {
+      // 3c. destroy the original molecule
+      for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
+        World::getInstance().destroyAtom(*AtomRunner);
+      World::getInstance().destroyMolecule(mol);
+    }
+
+    {
+      // 3d. convert to old Ids and correct fathers (AddCopyAtom sets father to original atom, which has been destroyed).
+      vector <atom *> allatoms = World::getInstance().getAllAtoms();
+      for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
+        World::getInstance().changeAtomId((*AtomRunner)->getId(), newtooldlist[(*AtomRunner)->getId()]);
+        (*AtomRunner)->father = *AtomRunner;
+      }
+    }
   }
 
@@ -134,5 +164,5 @@
   MolecularWalker->Leaf = NULL;
   delete(MolecularWalker);
-  DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
+  DoLog(1) && (Log() << Verbose(1) << "I scanned " << molecules->ListOfMolecules.size() << " molecules." << endl);
 
   return Action::state_ptr(UndoState);
@@ -154,4 +184,5 @@
   {
     // construct the old state
+    MoleculeListClass *molecules = World::getInstance().getMolecules();
     molecule *mol = NULL;
     for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
@@ -159,6 +190,9 @@
       if (mol->getId() != (*iter).first)
         World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
-      for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter)
-        mol->AddAtom(World::getInstance().getAtom(AtomById(*atomiter)));
+      for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter) {
+        atom *Walker = World::getInstance().getAtom(AtomById(*atomiter));
+        mol->AddAtom(Walker);
+      }
+      molecules->insert(mol);
     }
   }
