Index: src/Actions/MoleculeAction/ForceAnnealingAction.cpp
===================================================================
--- src/Actions/MoleculeAction/ForceAnnealingAction.cpp	(revision 6145577aecddb746ff0590fdb34d1f67d8064deb)
+++ src/Actions/MoleculeAction/ForceAnnealingAction.cpp	(revision c87d6e6a6c44b5378001040941957791e7ea0aa3)
@@ -90,7 +90,13 @@
           iter != World::getInstance().endAtomSelection();
           ++iter)
-        UndoInfo[i].push_back(AtomicInfo(*(iter->second), CurrentStep-i));
+          UndoInfo[i].push_back(AtomicInfo(*(iter->second), CurrentStep-i));
     }
   }
+  std::vector<int> UndoTrajectorySize;
+  UndoTrajectorySize.reserve(set.size());
+  for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
+      iter != World::getInstance().endAtomSelection();
+      ++iter)
+    UndoTrajectorySize.push_back(iter->second->getTrajectorySize());
 
   // instantiate optimizer
@@ -129,5 +135,5 @@
 
   MoleculeForceAnnealingState *UndoState =
-    new MoleculeForceAnnealingState(UndoInfo, RedoInfo, params);
+    new MoleculeForceAnnealingState(UndoInfo, UndoTrajectorySize, RedoInfo, params);
 
   return ActionState::ptr(UndoState);
@@ -139,7 +145,19 @@
   const size_t CurrentStep = WorldTime::getInstance().getTime();
 
-  // set stored old state
-  for (int i=0;i<2;++i)
-    SetAtomsFromAtomicInfo(state->UndoInfo[i], CurrentStep-i);
+  // set stored old state and remove current one from trajectory if we set it
+  for (int i=0;i<2;++i) {
+    for(size_t j=0;j<state->UndoInfo[i].size();++j) {
+      const AtomicInfo &_atominfo = state->UndoInfo[i][j];
+      const atomId_t id = _atominfo.getId();
+      atom * const _atom = World::getInstance().getAtom(AtomById(id));
+      ASSERT( _atom != NULL,
+          "MoleCuilder::SetAtomsFromAtomicInfo() - cannot find atom with id "
+          +toString(id)+" in the world.");
+      if (state->UndoTrajectorySize[j] > CurrentStep-i)
+        _atominfo.setAtom( *_atom, CurrentStep-i );
+      else
+        _atom->removeSteps(state->UndoTrajectorySize[j], CurrentStep-i);
+    }
+  }
 
   return ActionState::ptr(_state);
Index: src/Actions/MoleculeAction/ForceAnnealingAction.def
===================================================================
--- src/Actions/MoleculeAction/ForceAnnealingAction.def	(revision 6145577aecddb746ff0590fdb34d1f67d8064deb)
+++ src/Actions/MoleculeAction/ForceAnnealingAction.def	(revision c87d6e6a6c44b5378001040941957791e7ea0aa3)
@@ -32,6 +32,6 @@
 (DummyValidator<bool>())
 
-#define statetypes (std::vector< std::vector<AtomicInfo> >)(std::vector< std::vector<AtomicInfo> >)
-#define statereferences (UndoInfo)(RedoInfo)
+#define statetypes (std::vector< std::vector<AtomicInfo> >)(std::vector<int>)(std::vector< std::vector<AtomicInfo> >)
+#define statereferences (UndoInfo)(UndoTrajectorySize)(RedoInfo)
 
 // some defines for all the names, you may use ACTION, STATE and PARAMS
Index: tests/regression/Molecules/ForceAnnealing/post/test-undo.conf
===================================================================
--- tests/regression/Molecules/ForceAnnealing/post/test-undo.conf	(revision 6145577aecddb746ff0590fdb34d1f67d8064deb)
+++ tests/regression/Molecules/ForceAnnealing/post/test-undo.conf	(revision c87d6e6a6c44b5378001040941957791e7ea0aa3)
@@ -82,14 +82,2 @@
 Ion_Type2_2	8.532785963	4.158586027	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 9
 Ion_Type2_3	7.283585982	3.275186040	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 10
-
-Ion_Type1_1	9.782085945	2.645886050	2.645886050	0 # molecule nr 0
-Ion_Type1_2	9.782085945	2.645886050	4.425886024	0 # molecule nr 1
-Ion_Type1_3	10.672039608	3.904536878	3.535886037	0 # molecule nr 2
-Ion_Type1_4	8.532785963	4.787886018	2.645886050	0 # molecule nr 3
-Ion_Type1_5	8.532785963	4.787886018	4.425886024	0 # molecule nr 4
-Ion_Type1_6	6.393632318	3.904536877	3.535886037	0 # molecule nr 5
-Ion_Type1_7	7.283585982	2.645886050	2.645886050	0 # molecule nr 6
-Ion_Type1_8	7.283585982	2.645886050	4.425886024	0 # molecule nr 7
-Ion_Type2_1	9.782085945	3.275186040	3.535886037	0 # molecule nr 8
-Ion_Type2_2	8.532785963	4.158586027	3.535886037	0 # molecule nr 9
-Ion_Type2_3	7.283585982	3.275186040	3.535886037	0 # molecule nr 10
Index: tests/regression/Molecules/ForceAnnealing/post/test-undo2.conf
===================================================================
--- tests/regression/Molecules/ForceAnnealing/post/test-undo2.conf	(revision c87d6e6a6c44b5378001040941957791e7ea0aa3)
+++ tests/regression/Molecules/ForceAnnealing/post/test-undo2.conf	(revision c87d6e6a6c44b5378001040941957791e7ea0aa3)
@@ -0,0 +1,95 @@
+# ParallelCarParinello - main configuration file - created with molecuilder
+
+mainname	pcp	# programm name (for runtime files)
+defaultpath	not specified	# where to put files during runtime
+pseudopotpath	not specified	# where to find pseudopotentials
+
+ProcPEGamma	8	# for parallel computing: share constants
+ProcPEPsi	1	# for parallel computing: share wave functions
+DoOutVis	0	# Output data for OpenDX
+DoOutMes	1	# Output data for measurements
+DoOutOrbitals	0	# Output all Orbitals
+DoOutCurr	0	# Ouput current density for OpenDx
+DoOutNICS	0	# Output Nucleus independent current shieldings
+DoPerturbation	0	# Do perturbation calculate and determine susceptibility and shielding
+DoFullCurrent	0	# Do full perturbation
+DoConstrainedMD	0	# Do perform a constrained (>0, relating to current MD step) instead of unconstrained (0) MD
+Thermostat	Berendsen	2.5	# Which Thermostat and its parameters to use in MD case.
+CommonWannier	0	# Put virtual centers at indivual orbits, all common, merged by variance, to grid point, to cell center
+SawtoothStart	0.01	# Absolute value for smooth transition at cell border 
+VectorPlane	0	# Cut plane axis (x, y or z: 0,1,2) for two-dim current vector plot
+VectorCut	0	# Cut plane axis value
+AddGramSch	1	# Additional GramSchmidtOrtogonalization to be safe
+Seed		1	# initial value for random seed for Psi coefficients
+
+MaxOuterStep	10	# number of MolecularDynamics/Structure optimization steps
+Deltat	0.01	# time per MD step
+OutVisStep	10	# Output visual data every ...th step
+OutSrcStep	5	# Output "restart" data every ..th step
+TargetTemp	0.000950045	# Target temperature
+MaxPsiStep	3	# number of Minimisation steps per state (0 - default)
+EpsWannier	1e-07	# tolerance value for spread minimisation of orbitals
+
+# Values specifying when to stop
+MaxMinStep	100	# Maximum number of steps
+RelEpsTotalE	1e-07	# relative change in total energy
+RelEpsKineticE	1e-05	# relative change in kinetic energy
+MaxMinStopStep	10	# check every ..th steps
+MaxMinGapStopStep	1	# check every ..th steps
+
+# Values specifying when to stop for INIT, otherwise same as above
+MaxInitMinStep	100	# Maximum number of steps
+InitRelEpsTotalE	1e-05	# relative change in total energy
+InitRelEpsKineticE	0.0001	# relative change in kinetic energy
+InitMaxMinStopStep	10	# check every ..th steps
+InitMaxMinGapStopStep	1	# check every ..th steps
+
+BoxLength			# (Length of a unit cell)
+20	
+0	20	
+0	0	20	
+
+ECut		128	# energy cutoff for discretization in Hartrees
+MaxLevel	5	# number of different levels in the code, >=2
+Level0Factor	2	# factor by which node number increases from S to 0 level
+RiemannTensor	0	# (Use metric)
+PsiType		0	# 0 - doubly occupied, 1 - SpinUp,SpinDown
+MaxPsiDouble	10	# here: specifying both maximum number of SpinUp- and -Down-states
+PsiMaxNoUp	10	# here: specifying maximum number of SpinUp-states
+PsiMaxNoDown	10	# here: specifying maximum number of SpinDown-states
+AddPsis		0	# Additional unoccupied Psis for bandgap determination
+
+RCut		20	# R-cut for the ewald summation
+StructOpt	0	# Do structure optimization beforehand
+IsAngstroem	1	# 0 - Bohr, 1 - Angstroem
+RelativeCoord	0	# whether ion coordinates are relative (1) or absolute (0)
+MaxTypes	2	# maximum number of different ion types
+
+# Ion type data (PP = PseudoPotential, Z = atomic number)
+#Ion_TypeNr.	Amount	Z	RGauss	L_Max(PP)L_Loc(PP)IonMass	# chemical name, symbol
+Ion_Type1	8	1	1.0	3	3	1.00800000000	Hydrogen	H
+Ion_Type2	3	6	1.0	3	3	12.01100000000	Carbon	C
+#Ion_TypeNr._Nr.R[0]    R[1]    R[2]    MoveType (0 MoveIon, 1 FixedIon)
+Ion_Type1_1	9.782085945	2.645886050	2.645886050	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 0
+Ion_Type1_2	9.782085945	2.645886050	4.425886024	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 1
+Ion_Type1_3	10.672039608	3.904536878	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 2
+Ion_Type1_4	8.532785963	4.787886018	2.645886050	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 3
+Ion_Type1_5	8.532785963	4.787886018	4.425886024	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 4
+Ion_Type1_6	6.393632318	3.904536877	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 5
+Ion_Type1_7	7.283585982	2.645886050	2.645886050	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 6
+Ion_Type1_8	7.283585982	2.645886050	4.425886024	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 7
+Ion_Type2_1	9.782085945	3.275186040	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 8
+Ion_Type2_2	8.532785963	4.158586027	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 9
+Ion_Type2_3	7.283585982	3.275186040	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 10
+
+Ion_Type1_1	9.782085945	2.645886050	2.645886050	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 0
+Ion_Type1_2	9.782085945	2.645886050	4.425886024	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 1
+Ion_Type1_3	10.672039608	3.904536878	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 2
+Ion_Type1_4	8.532785963	4.787886018	2.645886050	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 3
+Ion_Type1_5	8.532785963	4.787886018	4.425886024	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 4
+Ion_Type1_6	6.393632318	3.904536877	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 5
+Ion_Type1_7	7.283585982	2.645886050	2.645886050	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 6
+Ion_Type1_8	7.283585982	2.645886050	4.425886024	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 7
+Ion_Type2_1	9.782085945	3.275186040	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 8
+Ion_Type2_2	8.532785963	4.158586027	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 9
+Ion_Type2_3	7.283585982	3.275186040	3.535886037	0	1.000000e-01	0.000000e+00	0.000000e+00	 # molecule nr 10
Index: tests/regression/Molecules/ForceAnnealing/pre/test2.conf
===================================================================
--- tests/regression/Molecules/ForceAnnealing/pre/test2.conf	(revision c87d6e6a6c44b5378001040941957791e7ea0aa3)
+++ tests/regression/Molecules/ForceAnnealing/pre/test2.conf	(revision c87d6e6a6c44b5378001040941957791e7ea0aa3)
@@ -0,0 +1,95 @@
+# ParallelCarParinello - main configuration file - created with molecuilder
+
+mainname	pcp	# programm name (for runtime files)
+defaultpath	not specified	# where to put files during runtime
+pseudopotpath	not specified	# where to find pseudopotentials
+
+ProcPEGamma	8	# for parallel computing: share constants
+ProcPEPsi	1	# for parallel computing: share wave functions
+DoOutVis	0	# Output data for OpenDX
+DoOutMes	1	# Output data for measurements
+DoOutOrbitals	0	# Output all Orbitals
+DoOutCurr	0	# Ouput current density for OpenDx
+DoOutNICS	0	# Output Nucleus independent current shieldings
+DoPerturbation	0	# Do perturbation calculate and determine susceptibility and shielding
+DoFullCurrent	0	# Do full perturbation
+DoConstrainedMD	0	# Do perform a constrained (>0, relating to current MD step) instead of unconstrained (0) MD
+Thermostat	Berendsen	2.5	# Which Thermostat and its parameters to use in MD case.
+CommonWannier	0	# Put virtual centers at indivual orbits, all common, merged by variance, to grid point, to cell center
+SawtoothStart	0.01	# Absolute value for smooth transition at cell border 
+VectorPlane	0	# Cut plane axis (x, y or z: 0,1,2) for two-dim current vector plot
+VectorCut	0	# Cut plane axis value
+AddGramSch	1	# Additional GramSchmidtOrtogonalization to be safe
+Seed		1	# initial value for random seed for Psi coefficients
+
+MaxOuterStep	10	# number of MolecularDynamics/Structure optimization steps
+Deltat	0.01	# time per MD step
+OutVisStep	10	# Output visual data every ...th step
+OutSrcStep	5	# Output "restart" data every ..th step
+TargetTemp	0.000950045	# Target temperature
+MaxPsiStep	0	# number of Minimisation steps per state (0 - default)
+EpsWannier	1e-07	# tolerance value for spread minimisation of orbitals
+
+# Values specifying when to stop
+MaxMinStep	100	# Maximum number of steps
+RelEpsTotalE	1e-07	# relative change in total energy
+RelEpsKineticE	1e-05	# relative change in kinetic energy
+MaxMinStopStep	0	# check every ..th steps
+MaxMinGapStopStep	0	# check every ..th steps
+
+# Values specifying when to stop for INIT, otherwise same as above
+MaxInitMinStep	100	# Maximum number of steps
+InitRelEpsTotalE	1e-05	# relative change in total energy
+InitRelEpsKineticE	0.0001	# relative change in kinetic energy
+InitMaxMinStopStep	0	# check every ..th steps
+InitMaxMinGapStopStep	0	# check every ..th steps
+
+BoxLength			# (Length of a unit cell)
+20	
+0	20	
+0	0	20	
+
+ECut		128	# energy cutoff for discretization in Hartrees
+MaxLevel	5	# number of different levels in the code, >=2
+Level0Factor	2	# factor by which node number increases from S to 0 level
+RiemannTensor	0	# (Use metric)
+PsiType		0	# 0 - doubly occupied, 1 - SpinUp,SpinDown
+MaxPsiDouble	0	# here: specifying both maximum number of SpinUp- and -Down-states
+PsiMaxNoUp	0	# here: specifying maximum number of SpinUp-states
+PsiMaxNoDown	0	# here: specifying maximum number of SpinDown-states
+AddPsis		0	# Additional unoccupied Psis for bandgap determination
+
+RCut		20	# R-cut for the ewald summation
+StructOpt	0	# Do structure optimization beforehand
+IsAngstroem	1	# 0 - Bohr, 1 - Angstroem
+RelativeCoord	0	# whether ion coordinates are relative (1) or absolute (0)
+MaxTypes	2	# maximum number of different ion types
+
+# Ion type data (PP = PseudoPotential, Z = atomic number)
+#Ion_TypeNr.	Amount	Z	RGauss	L_Max(PP)L_Loc(PP)IonMass	# chemical name, symbol
+Ion_Type1	8	1	1.0	3	3	1.00800000000	Hydrogen	H
+Ion_Type2	3	6	1.0	3	3	12.01100000000	Carbon	C
+#Ion_TypeNr._Nr.R[0]    R[1]    R[2]    MoveType (0 MoveIon, 1 FixedIon)
+Ion_Type2_1	9.782085945	3.275186040	3.535886037	0 0.1 0. 0.
+Ion_Type2_2	8.532785963	4.158586027	3.535886037	0 0.1 0. 0.
+Ion_Type2_3	7.283585982	3.275186040	3.535886037	0 0.1 0. 0.
+Ion_Type1_1	9.782085945	2.645886050	2.645886050	0 0.1 0. 0.
+Ion_Type1_2	9.782085945	2.645886050	4.425886024	0 0.1 0. 0.
+Ion_Type1_3	10.672039608	3.904536878	3.535886037	0 0.1 0. 0.
+Ion_Type1_4	8.532785963	4.787886018	2.645886050	0 0.1 0. 0.
+Ion_Type1_5	8.532785963	4.787886018	4.425886024	0 0.1 0. 0.
+Ion_Type1_6	6.393632318	3.904536877	3.535886037	0 0.1 0. 0.
+Ion_Type1_7	7.283585982	2.645886050	2.645886050	0 0.1 0. 0.
+Ion_Type1_8	7.283585982	2.645886050	4.425886024	0 0.1 0. 0.
+
+Ion_Type2_1	9.782085945	3.275186040	3.535886037	0 0.1 0. 0.
+Ion_Type2_2	8.532785963	4.158586027	3.535886037	0 0.1 0. 0.
+Ion_Type2_3	7.283585982	3.275186040	3.535886037	0 0.1 0. 0.
+Ion_Type1_1	9.782085945	2.645886050	2.645886050	0 0.1 0. 0.
+Ion_Type1_2	9.782085945	2.645886050	4.425886024	0 0.1 0. 0.
+Ion_Type1_3	10.672039608	3.904536878	3.535886037	0 0.1 0. 0.
+Ion_Type1_4	8.532785963	4.787886018	2.645886050	0 0.1 0. 0.
+Ion_Type1_5	8.532785963	4.787886018	4.425886024	0 0.1 0. 0.
+Ion_Type1_6	6.393632318	3.904536877	3.535886037	0 0.1 0. 0.
+Ion_Type1_7	7.283585982	2.645886050	2.645886050	0 0.1 0. 0.
+Ion_Type1_8	7.283585982	2.645886050	4.425886024	0 0.1 0. 0.
Index: tests/regression/Molecules/ForceAnnealing/testsuite-molecules-force-annealing.at
===================================================================
--- tests/regression/Molecules/ForceAnnealing/testsuite-molecules-force-annealing.at	(revision 6145577aecddb746ff0590fdb34d1f67d8064deb)
+++ tests/regression/Molecules/ForceAnnealing/testsuite-molecules-force-annealing.at	(revision c87d6e6a6c44b5378001040941957791e7ea0aa3)
@@ -31,5 +31,6 @@
 
 
-AT_SETUP([Molecules - Force Annealing with Undo])
+# here, we have only one step in the trajectories
+AT_SETUP([Molecules - Force Annealing with Undo I])
 AT_KEYWORDS([molecules force-annealing undo])
 
@@ -40,4 +41,18 @@
 AT_CHECK([../../molecuilder -i $file --select-all-atoms --step-world-time 1 --force-annealing --forces-file test.forces --steps 1 --undo], 0, [stdout], [stderr])
 AT_CHECK([diff $file ${abs_top_srcdir}/tests/regression/Molecules/ForceAnnealing/post/test-undo.conf], 0, [ignore], [ignore])
+
+AT_CLEANUP
+
+
+# here two steps are present already
+AT_SETUP([Molecules - Force Annealing with Undo II])
+AT_KEYWORDS([molecules force-annealing undo])
+
+file=test2.conf
+AT_CHECK([/bin/cp -f ${abs_top_srcdir}/tests/regression/Molecules/ForceAnnealing/pre/test2.conf $file], 0)
+AT_CHECK([/bin/cp -f ${abs_top_srcdir}/tests/regression/Molecules/ForceAnnealing/pre/test.forces .], 0)
+AT_CHECK([chmod u+w $file], 0)
+AT_CHECK([../../molecuilder -i $file --select-all-atoms --set-world-time 1 --force-annealing --forces-file test.forces --steps 1 --undo], 0, [stdout], [stderr])
+AT_CHECK([diff $file ${abs_top_srcdir}/tests/regression/Molecules/ForceAnnealing/post/test-undo2.conf], 0, [ignore], [ignore])
 
 AT_CLEANUP
