Index: src/Actions/AtomAction/AddAction.cpp
===================================================================
--- src/Actions/AtomAction/AddAction.cpp	(revision 26b4d62081a948abd7e73ffa88b6aec3eb1e5234)
+++ src/Actions/AtomAction/AddAction.cpp	(revision ceaab1dbd47046505deba359f3a96512689de872)
@@ -55,17 +55,51 @@
 #include "Action_impl_pre.hpp"
 /** =========== define the function ====================== */
+
+atom * getNewAtom(const AtomAddAction::AtomAddParameters &_params)
+{
+  atom * first = World::getInstance().createAtom();
+  first->setType(_params.elemental.get());
+  first->setPosition(_params.position.get());
+
+  return first;
+}
+
+std::vector<atomId_t> createAtoms(
+    const AtomAddAction::AtomAddParameters &_params,
+    std::vector<molecule *> &_molecules)
+{
+  std::vector<atomId_t> ids;
+  if (!_molecules.empty()) {
+    if (_molecules.size() == 1) {
+        atom *first = getNewAtom(_params);
+        molecule *mol = *_molecules.begin();
+        LOG(1, "Adding new atom with element " << first->getType()->getName()
+            << " at " << (first->getPosition()) << " to selected molecule "
+            << mol->getName()+".");
+        mol->AddAtom(first);
+      ids.push_back(first->getId());
+    }
+  } else {
+    atom *first = getNewAtom(_params);
+    molecule *mol = World::getInstance().createMolecule();
+    mol->setName("none");
+    mol->AddAtom(first);
+    LOG(1, "Adding new atom with element " << first->getType()->getName()
+        << " at " << (first->getPosition()) << " to new molecule.");
+    ids.push_back(first->getId());
+  }
+
+  return ids;
+}
+
 ActionState::ptr AtomAddAction::performCall() {
   // execute action
-  atom * first = World::getInstance().createAtom();
-  first->setType(params.elemental.get());
-  first->setPosition(params.position.get());
-  LOG(1, "Adding new atom with element " << first->getType()->getName() << " at " << (first->getPosition()) << ".");
-  // TODO: remove when all of World's atoms are stored.
-  std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
-  if (!molecules.empty()) {
-    std::vector<molecule *>::iterator iter = molecules.begin();
-    (*iter)->AddAtom(first);
-  }
-  return ActionState::ptr(new AtomAddState(first->getId(), params));
+  std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
+  std::vector<atomId_t> ids = createAtoms(params, molecules);
+
+  if (molecules.size() > 1)
+     return Action::failure;
+  else
+    return ActionState::ptr(new AtomAddState(ids, params));
 }
 
@@ -73,6 +107,9 @@
   AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
 
-  LOG(1, "Removing atom with id " << state->id << ".");
-  World::getInstance().destroyAtom(state->id);
+  for (std::vector<atomId_t>::const_iterator iter = state->ids.begin();
+      iter != state->ids.end(); ++iter) {
+    LOG(1, "Removing atom with id " << *iter << ".");
+    World::getInstance().destroyAtom(*iter);
+  }
 
   return ActionState::ptr(_state);
@@ -82,21 +119,41 @@
   AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
 
-  atom * first = World::getInstance().createAtom();
-  first->setType(state->params.elemental.get());
-  first->setPosition(state->params.position.get());
-  LOG(1, "Re-adding new atom with element " << state->params.elemental.get()->getName() << " at " << state->params.position.get() << ".");
-  // TODO: remove when all of World's atoms are stored.
-  std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
-  if (!molecules.empty()) {
-    std::vector<molecule *>::iterator iter = molecules.begin();
-    (*iter)->AddAtom(first);
+  std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
+  std::vector<atomId_t> newids = createAtoms(params, molecules);
+
+  if (newids.size() != state->ids.size()) {
+    STATUS("Could not recreate all atoms after undo.");
+    for (std::vector<atomId_t>::const_iterator iter = newids.begin(); iter != newids.end();++iter)
+      World::getInstance().destroyAtom(*iter);
+    return Action::failure;
   }
-  if (first->getId() != state->id)
-    if (!first->changeId(state->id)) {
-      STATUS("Could not change atom id "+toString(first->getId())+"->"+toString(state->id)+".");
-      return Action::failure;
-    }
 
-  return ActionState::ptr(_state);
+  std::vector<atomId_t>::const_iterator newiter = newids.begin();
+  std::vector<atomId_t>::const_iterator olditer = state->ids.begin();
+  bool status=true;
+  for (; newiter != newids.end(); ++newiter, ++olditer) {
+    atom * first = World::getInstance().getAtom(AtomById(*newiter));
+    ASSERT( first != NULL,
+        "AtomAddAction::performRedo() - re-created atom not present?");
+    if (first->getId() != *olditer)
+      if (!first->changeId(*olditer)) {
+        STATUS("Could not change atom id "+toString(first->getId())+"->"+toString(*olditer)+".");
+        // remove all created atoms
+        for (std::vector<atomId_t>::const_iterator iter = state->ids.begin(); iter != olditer;++iter)
+          World::getInstance().destroyAtom(*iter);
+        olditer = state->ids.end();
+        for (std::vector<atomId_t>::const_iterator iter = newiter; iter != newids.end();++iter)
+          World::getInstance().destroyAtom(*iter);
+        status = false;
+        break;
+      }
+  }
+  ASSERT( olditer == state->ids.end(),
+      "AtomAddAction::performRedo() - after all unequal amount of ids in new and old?");
+
+  if (!status) {
+    return Action::failure;
+  } else
+    return ActionState::ptr(_state);
 }
 
