Index: src/Actions/AnalysisAction/SurfaceCorrelationAction.cpp
===================================================================
--- src/Actions/AnalysisAction/SurfaceCorrelationAction.cpp	(revision 51769f1ad434c697ff72558611b3163dbe892aa8)
+++ src/Actions/AnalysisAction/SurfaceCorrelationAction.cpp	(revision 5f1d5b842f0b34e83a8b8253e98b4bb0defe850e)
@@ -117,4 +117,5 @@
   // find biggest molecule
   std::vector<molecule*> molecules = World::getInstance().getSelectedMolecules();
+  std::cout << "There are " << molecules.size() << " selected molecules." << std::endl;
   LCList = new LinkedCell(Boundary, LCWidth);
   FindNonConvexBorder(Boundary, TesselStruct, LCList, radius, NULL);
Index: src/Actions/FragmentationAction/SubgraphDissectionAction.cpp
===================================================================
--- src/Actions/FragmentationAction/SubgraphDissectionAction.cpp	(revision 51769f1ad434c697ff72558611b3163dbe892aa8)
+++ src/Actions/FragmentationAction/SubgraphDissectionAction.cpp	(revision 5f1d5b842f0b34e83a8b8253e98b4bb0defe850e)
@@ -15,4 +15,5 @@
 #include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
 #include "Actions/ActionRegistry.hpp"
+#include "Descriptors/AtomIdDescriptor.hpp"
 #include "Descriptors/MoleculeDescriptor.hpp"
 
@@ -22,7 +23,7 @@
 #include "config.hpp"
 #include "Helpers/Log.hpp"
+#include "Helpers/Verbose.hpp"
 #include "molecule.hpp"
 #include "stackclass.hpp"
-#include "verbose.hpp"
 #include "World.hpp"
 
@@ -35,4 +36,16 @@
 #include "UIElements/Dialog.hpp"
 #include "Actions/ValueStorage.hpp"
+
+// memento to remember the state when undoing
+
+typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
+
+class FragmentationSubgraphDissectionState : public ActionState {
+public:
+  FragmentationSubgraphDissectionState(const MolAtomList &_moleculelist) :
+   moleculelist(_moleculelist)
+  {}
+  MolAtomList moleculelist;
+};
 
 const char FragmentationSubgraphDissectionAction::NAME[] = "subgraph-dissect";
@@ -63,9 +76,21 @@
   DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
 
+  // first create undo state
+  MolAtomList moleculelist;
+  vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
+  for (vector<molecule *>::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) {
+    std::vector<atomId_t> atomlist;
+    atomlist.resize((*moliter)->size());
+    for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) {
+      atomlist.push_back((*atomiter)->getId());
+    }
+    moleculelist.insert( std::pair< moleculeId_t, std::vector<atomId_t> > ((*moliter)->getId(), atomlist));
+  }
+  FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist);
+
   MoleculeListClass *molecules = World::getInstance().getMolecules();
   config * const configuration = World::getInstance().getConfig();
 
   // 0a. remove all present molecules
-  vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
   for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
     molecules->erase(*MolRunner);
@@ -75,9 +100,11 @@
   // 0b. remove all bonds and construct a molecule with all atoms
   molecule *mol = World::getInstance().createMolecule();
-  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);
-    mol->AddAtom(*AtomRunner);
+  {
+    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);
+      mol->AddAtom(*AtomRunner);
+    }
   }
 
@@ -100,104 +127,75 @@
   delete(BackEdgeStack);
   if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
-    World::getInstance().destroyMolecule(mol);
+    //World::getInstance().destroyMolecule(mol);
     DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
     return Action::failure;
   }
-<<<<<<< HEAD
-
-  // 3. dissect (the following construct is needed to have the atoms not in the order of the DFS, but in
-  // the original one as parsed in)
-  // TODO: Optimize this, when molecules just contain pointer list of global atoms!
-
-  // 4a. create array of molecules to fill
-  const int MolCount = Subgraphs->next->Count();
-  char number[MAXSTRINGSIZE];
-  molecule **moleculelist = new molecule *[MolCount];
-  MoleculeLeafClass *MolecularWalker = Subgraphs;
-  for (int i=0;i<MolCount;i++) {
-    MolecularWalker = MolecularWalker->next;
-    moleculelist[i] = World::getInstance().createMolecule();
-    moleculelist[i]->ActiveFlag = true;
-    strncpy(moleculelist[i]->name, mol->name, MAXSTRINGSIZE);
-    if (MolCount > 1) {
-      sprintf(number, "-%d", i+1);
-      strncat(moleculelist[i]->name, number, MAXSTRINGSIZE - strlen(mol->name) - 1);
-    }
-    DoLog(1) && (Log() << Verbose(1) << "MolName is " << moleculelist[i]->name << ", id is " << moleculelist[i]->getId() << endl);
-    for (molecule::iterator iter = MolecularWalker->Leaf->begin(); iter != MolecularWalker->Leaf->end(); ++iter) {
-      DoLog(1) && (Log() << Verbose(1) << **iter << endl);
-    }
-    molecules->insert(moleculelist[i]);
-  }
-
-  // 4b. create and fill map of which atom is associated to which connected molecule (note, counting starts at 1)
-  int FragmentCounter = 0;
-  map<int, atom *> AtomToFragmentMap;
-  MolecularWalker = Subgraphs;
-  while (MolecularWalker->next != NULL) {
-    MolecularWalker = MolecularWalker->next;
-    for (molecule::iterator iter = MolecularWalker->Leaf->begin(); !MolecularWalker->Leaf->empty(); iter = MolecularWalker->Leaf->begin()) {
-      atom * Walker = *iter;
-      DoLog(1) && (Log() << Verbose(1) << "Re-linking " << Walker << "..." << endl);
-      MolecularWalker->Leaf->erase(iter);
-      moleculelist[FragmentCounter]->AddAtom(Walker);    // counting starts at 1
-    }
-    FragmentCounter++;
-  }
-  // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms.
-  // 4d. destroy the original molecule
-=======
-  int FragmentCounter = Subgraphs->Count();
+  int FragmentCounter = Subgraphs->next->Count();
 
   // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
-  // 3. destroy the original molecule
->>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
-  for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
-    World::getInstance().destroyAtom(*AtomRunner);
-  World::getInstance().destroyMolecule(mol);
-
-<<<<<<< HEAD
-  // 4d. we don't need to redo bonds, as they are connected subgraphs and still maintain their ListOfBonds, but we have to remove them from first..last list
-  // TODO: check whether this is really not needed anymore
-  // 4e. free Leafs
-  MolecularWalker = Subgraphs;
-=======
+  {
+    // 3a. destroy the original molecule
+    for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
+      World::getInstance().destroyAtom(*AtomRunner);
+    World::getInstance().destroyMolecule(mol);
+    // 3b. 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) {
+      (*AtomRunner)->father = *AtomRunner;
+    }
+  }
+
   // 4. free Leafs
   MoleculeLeafClass *MolecularWalker = Subgraphs;
->>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
   while (MolecularWalker->next != NULL) {
+    MolecularWalker->Leaf = NULL;
     MolecularWalker = MolecularWalker->next;
     delete(MolecularWalker->previous);
   }
+  MolecularWalker->Leaf = NULL;
   delete(MolecularWalker);
-<<<<<<< HEAD
-  delete[](moleculelist);
-=======
->>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
   DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
 
-  return Action::success;
+  return Action::state_ptr(UndoState);
 }
 
 Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
-//  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
-
-  return Action::failure;
-//  string newName = state->mol->getName();
-//  state->mol->setName(state->lastName);
-//
-//  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
+  FragmentationSubgraphDissectionState *state = assert_cast<FragmentationSubgraphDissectionState*>(_state.get());
+
+  {
+    // remove all present molecules
+    MoleculeListClass *molecules = World::getInstance().getMolecules();
+    vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
+    for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
+      molecules->erase(*MolRunner);
+      World::getInstance().destroyMolecule(*MolRunner);
+    }
+  }
+
+  {
+    // construct the old state
+    molecule *mol = NULL;
+    for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
+      mol = World::getInstance().createMolecule();
+      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)));
+    }
+  }
+
+  return Action::state_ptr(_state);
 }
 
 Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
-  return Action::failure;
+  return performCall();
 }
 
 bool FragmentationSubgraphDissectionAction::canUndo() {
-  return false;
+  return true;
 }
 
 bool FragmentationSubgraphDissectionAction::shouldUndo() {
-  return false;
+  return true;
 }
 
