Index: src/molecules.cpp
===================================================================
--- src/molecules.cpp	(revision 85bac07a96bad99841916279c5887226effb9305)
+++ src/molecules.cpp	(revision c30cda6abbb8ffa286b09e6111634efbf42891a6)
@@ -1156,10 +1156,20 @@
 {
   stringstream zeile1, zeile2;
+  int *DoubleList = (int *) Malloc(Nr*sizeof(int), "PrintPermutationMap: *DoubleList");
+  int doubles = 0;
+  for (int i=0;i<Nr;i++)
+    DoubleList[i] = 0;
   zeile1 << "PermutationMap: ";
   zeile2 << "                ";
   for (int i=0;i<Nr;i++) {
+    DoubleList[PermutationMap[i]->nr]++;
     zeile1 << i << " ";
     zeile2 << PermutationMap[i]->nr << " ";
   }
+  for (int i=0;i<Nr;i++)
+    if (DoubleList[i] > 1)
+    doubles++;
+ // *out << "Found " << doubles << " Doubles." << endl;
+  Free((void **)&DoubleList, "PrintPermutationMap: *DoubleList");
 //  *out << zeile1.str() << endl << zeile2.str() << endl;
 };
@@ -1194,12 +1204,14 @@
 double molecule::MinimiseConstrainedPotential(ofstream *out, atom **&PermutationMap, int startstep, int endstep, bool IsAngstroem)
 {
-  double Potential, OldPotential;
+  double Potential, OldPotential, OlderPotential;
   PermutationMap = (atom **) Malloc(AtomCount*sizeof(atom *), "molecule::MinimiseConstrainedPotential: **PermutationMap");
   DistanceMap **DistanceList = (DistanceMap **) Malloc(AtomCount*sizeof(DistanceMap *), "molecule::MinimiseConstrainedPotential: **DistanceList");
   DistanceMap::iterator *DistanceIterators = (DistanceMap::iterator *) Malloc(AtomCount*sizeof(DistanceMap::iterator), "molecule::MinimiseConstrainedPotential: *DistanceIterators");
   int *DoubleList = (int *) Malloc(AtomCount*sizeof(int), "molecule::MinimiseConstrainedPotential: *DoubleList");
+  DistanceMap::iterator *StepList = (DistanceMap::iterator *) Malloc(AtomCount*sizeof(DistanceMap::iterator), "molecule::MinimiseConstrainedPotential: *StepList");
   double constants[3];
+  int round;
   atom *Walker = NULL, *Runner = NULL, *Sprinter = NULL;
-  DistanceMap::iterator Rider;
+  DistanceMap::iterator Rider, Strider;
   
   /// Minimise the potential
@@ -1211,5 +1223,5 @@
   *out << Verbose(1) << "Creating the distance list ... " << endl;
   for (int i=AtomCount; i--;) {
-    DoubleList[i] = 0;  // store's for how many atoms in startstep this atom is a target in endstep
+    DoubleList[i] = 0;  // stores for how many atoms in startstep this atom is a target in endstep
     DistanceList[i] = new DistanceMap;    // is the distance sorted target list per atom
     DistanceList[i]->clear();
@@ -1229,4 +1241,5 @@
   while (Walker->next != end) {
     Walker = Walker->next;
+    StepList[Walker->nr] = DistanceList[Walker->nr]->begin();    // stores the step to the next iterator that could be a possible next target
     PermutationMap[Walker->nr] = DistanceList[Walker->nr]->begin()->second;   // always pick target with the smallest distance
     DoubleList[DistanceList[Walker->nr]->begin()->second->nr]++;            // increase this target's source count (>1? not injective)
@@ -1253,14 +1266,12 @@
     } while ((DoubleList[NewBase->second->nr] != 0) && (NewBase != DistanceList[Walker->nr]->end()));
     if (NewBase != DistanceList[Walker->nr]->end()) {
-      DoubleList[DistanceIterators[Walker->nr]->second->nr]--;  // decrease the old entry in the doubles list
-      DoubleList[NewBase->second->nr]++;    // increase the old entry in the doubles list
       PermutationMap[Walker->nr] = NewBase->second;
       Potential = fabs(ConstrainedPotential(out, PermutationMap, startstep, endstep, constants, IsAngstroem));
       if (Potential > OldPotential) { // undo
-        DoubleList[NewBase->second->nr]--;
+        PermutationMap[Walker->nr] = DistanceIterators[Walker->nr]->second;
+      } else {  // do
+        DoubleList[DistanceIterators[Walker->nr]->second->nr]--;  // decrease the old entry in the doubles list
+        DoubleList[NewBase->second->nr]++;    // increase the old entry in the doubles list
         DistanceIterators[Walker->nr] = NewBase;
-        PermutationMap[Walker->nr] = DistanceIterators[Walker->nr]->second;
-        DoubleList[DistanceIterators[Walker->nr]->second->nr]++;
-      } else {
         OldPotential = Potential;
         *out << Verbose(3) << "Found a new permutation, new potential is " << OldPotential << "." << endl;
@@ -1277,62 +1288,75 @@
   // argument minimise the constrained potential in this injective PermutationMap
   *out << Verbose(1) << "Argument minimising the PermutationMap, at current potential " << OldPotential << " ... " << endl;
+  OldPotential = 1e+10;
+  round = 0;
   do {
-    Walker = start;
-    while (Walker->next != end) { // pick one
-      Walker = Walker->next;
-      PrintPermutationMap(out, PermutationMap, AtomCount);
-      Sprinter = DistanceIterators[Walker->nr]->second;   // store initial partner
-      DistanceIterators[Walker->nr]++;  // take next farther distance target
-      if (DistanceIterators[Walker->nr] == DistanceList[Walker->nr]->end()) // stop, before we run through the list and still on
-        break;
-      *out << Verbose(2) << "Current Walker: " << *Walker << " with old/next candidate " << *Sprinter << "/" << *DistanceIterators[Walker->nr]->second << "." << endl; 
-      Runner = start->next;
-      while(Runner != end) { // find the other one whose toes we might be stepping on (this target should be used by another already)
-        if (PermutationMap[Runner->nr] == DistanceIterators[Walker->nr]->second) {
-          *out << Verbose(2) << "Found the corresponding owner " << *Runner << " to " << *DistanceIterators[Walker->nr]->second << "." << endl;
+    *out << "Starting round " << ++round << " ... " << endl;
+    OlderPotential = OldPotential;
+    do {
+      Walker = start;
+      while (Walker->next != end) { // pick one
+        Walker = Walker->next;
+        PrintPermutationMap(out, PermutationMap, AtomCount);
+        Sprinter = DistanceIterators[Walker->nr]->second;   // store initial partner
+        Strider = DistanceIterators[Walker->nr];  //remember old iterator
+        DistanceIterators[Walker->nr] = StepList[Walker->nr];  
+        if (DistanceIterators[Walker->nr] == DistanceList[Walker->nr]->end()) {// stop, before we run through the list and still on
           break;
         }
-        Runner = Runner->next;
-      }
-      if (Runner != end) { // we found one a tripel
-        // then look in distance list for Sprinter
-        Rider = DistanceList[Runner->nr]->begin();
-        for (; Rider != DistanceList[Runner->nr]->end(); Rider++)
-          if (Rider->second == Sprinter)
-            break;
-        if (Rider != DistanceList[Runner->nr]->end()) { // if we have found one
-          *out << Verbose(2) << "Current Other: " << *Runner << " with old/next candidate " << *PermutationMap[Runner->nr] << "/" << *Rider->second << "." << endl; 
-          // exchange the second also 
-          PermutationMap[Walker->nr] = DistanceIterators[Walker->nr]->second; // put next farther distance into PermutationMap 
-          PermutationMap[Runner->nr] = Sprinter;  // and hand the old target to its respective owner
-          // calculate the new potential
-          PrintPermutationMap(out, PermutationMap, AtomCount);
-          *out << Verbose(2) << "Checking new potential ..." << endl;
-          Potential = ConstrainedPotential(out, PermutationMap, startstep, endstep, constants, IsAngstroem);
-          if (Potential > OldPotential) { // we made everything worse! Undo ...
-            *out << Verbose(3) << "Nay, made the potential worse: " << Potential << " vs. " << OldPotential << "!" << endl; 
-            // Undo for Walker
-            DistanceIterators[Walker->nr]--;  // take next farther distance target
-            PermutationMap[Walker->nr] = Sprinter;
-            // Undo for Runner (note, we haven't moved the iteration yet, we may use this)
-            PermutationMap[Runner->nr] = DistanceIterators[Runner->nr]->second;
-            Potential = OldPotential;
-          } else {
-            DistanceIterators[Runner->nr] = Rider;  // if successful also move the pointer in the iterator list
-            *out << Verbose(3) << "Found a better permutation, new potential is " << Potential << " vs." << OldPotential << "." << endl;
-            OldPotential = Potential;
+        //*out << Verbose(2) << "Current Walker: " << *Walker << " with old/next candidate " << *Sprinter << "/" << *DistanceIterators[Walker->nr]->second << "." << endl;
+        // find source of the new target
+        Runner = start->next;
+        while(Runner != end) { // find the source whose toes we might be stepping on (Walker's new target should be in use by another already)
+          if (PermutationMap[Runner->nr] == DistanceIterators[Walker->nr]->second) {
+            //*out << Verbose(2) << "Found the corresponding owner " << *Runner << " to " << *PermutationMap[Runner->nr] << "." << endl;
             break;
           }
-          if (Potential > constants[2]) {
-            cerr << "ERROR: The two-step permutation procedure did not maintain injectivity!" << endl;
+          Runner = Runner->next;
+        }
+        if (Runner != end) { // we found the other source
+          // then look in its distance list for Sprinter
+          Rider = DistanceList[Runner->nr]->begin();
+          for (; Rider != DistanceList[Runner->nr]->end(); Rider++)
+            if (Rider->second == Sprinter)
+              break;
+          if (Rider != DistanceList[Runner->nr]->end()) { // if we have found one
+            //*out << Verbose(2) << "Current Other: " << *Runner << " with old/next candidate " << *PermutationMap[Runner->nr] << "/" << *Rider->second << "." << endl; 
+            // exchange both 
+            PermutationMap[Walker->nr] = DistanceIterators[Walker->nr]->second; // put next farther distance into PermutationMap 
+            PermutationMap[Runner->nr] = Sprinter;  // and hand the old target to its respective owner
+            PrintPermutationMap(out, PermutationMap, AtomCount);
+            // calculate the new potential
+            //*out << Verbose(2) << "Checking new potential ..." << endl;
+            Potential = ConstrainedPotential(out, PermutationMap, startstep, endstep, constants, IsAngstroem);
+            if (Potential > OldPotential) { // we made everything worse! Undo ...
+              //*out << Verbose(3) << "Nay, made the potential worse: " << Potential << " vs. " << OldPotential << "!" << endl;
+              //*out << Verbose(3) << "Setting " << *Runner << "'s source to " << *DistanceIterators[Runner->nr]->second << "." << endl; 
+              // Undo for Runner (note, we haven't moved the iteration yet, we may use this)
+              PermutationMap[Runner->nr] = DistanceIterators[Runner->nr]->second;
+              // Undo for Walker
+              DistanceIterators[Walker->nr] = Strider;  // take next farther distance target
+              //*out << Verbose(3) << "Setting " << *Walker << "'s source to " << *DistanceIterators[Walker->nr]->second << "." << endl; 
+              PermutationMap[Walker->nr] = DistanceIterators[Walker->nr]->second;
+            } else {
+              DistanceIterators[Runner->nr] = Rider;  // if successful also move the pointer in the iterator list
+              *out << Verbose(3) << "Found a better permutation, new potential is " << Potential << " vs." << OldPotential << "." << endl;
+              OldPotential = Potential;
+            }
+            if (Potential > constants[2]) {
+              cerr << "ERROR: The two-step permutation procedure did not maintain injectivity!" << endl;
+              exit(255);
+            }
+            //*out << endl;
+          } else {
+            cerr << "ERROR: " << *Runner << " was not the owner of " << *Sprinter << "!" << endl;
             exit(255);
           }
-          *out << endl;
+        } else {
+          PermutationMap[Walker->nr] = DistanceIterators[Walker->nr]->second; // new target has no source!
         }
-      } else {
-        DistanceIterators[Walker->nr]--;  // take old distance target
-      }
-    }
-  } while (Walker->next != end);
+        StepList[Walker->nr]++; // take next farther distance target
+      }
+    } while (Walker->next != end);
+  } while ((OlderPotential - OldPotential) > 1e-3);
   *out << Verbose(1) << "done." << endl;
 
