Index: src/boundary.cpp
===================================================================
--- src/boundary.cpp	(revision b74f7dfc3cab111e2e5fbfe28dffa528131e6a7c)
+++ src/boundary.cpp	(revision 30d9e7c524259d13971388d69a8299194ae02715)
@@ -807,23 +807,22 @@
   double *M =  ReturnFullMatrixforSymmetric(filler->cell_size);
   double Rotations[NDIM*NDIM];
+  double *MInverse = InverseMatrix(M);
   Vector AtomTranslations;
   Vector FillerTranslations;
   Vector FillerDistance;
+  Vector Inserter;
   double FillIt = false;
   atom *Walker = NULL;
   bond *Binder = NULL;
-  int i = 0;
-  LinkedCell *LCList[List->ListOfMolecules.size()];
   double phi[NDIM];
-  class Tesselation *TesselStruct[List->ListOfMolecules.size()];
-
-  i=0;
+  map<molecule *, Tesselation *> TesselStruct;
+  map<molecule *, LinkedCell *> LCList;
+
   for (MoleculeList::iterator ListRunner = List->ListOfMolecules.begin(); ListRunner != List->ListOfMolecules.end(); ListRunner++) {
     Log() << Verbose(1) << "Pre-creating linked cell lists for molecule " << *ListRunner << "." << endl;
-    LCList[i] = new LinkedCell((*ListRunner), 10.); // get linked cell list
+    LCList[(*ListRunner)] = new LinkedCell((*ListRunner), 10.); // get linked cell list
     Log() << Verbose(1) << "Pre-creating tesselation for molecule " << *ListRunner << "." << endl;
-    TesselStruct[i] = NULL;
-    FindNonConvexBorder((*ListRunner), TesselStruct[i], (const LinkedCell *&)LCList[i], 5., NULL);
-    i++;
+    TesselStruct[(*ListRunner)] = NULL;
+    FindNonConvexBorder((*ListRunner), TesselStruct[(*ListRunner)], (const LinkedCell *&)LCList[(*ListRunner)], 5., NULL);
   }
 
@@ -846,81 +845,79 @@
 
   // go over [0,1]^3 filler grid
-  for (n[0] = 0; n[0] < N[0]; n[0]++)
-    for (n[1] = 0; n[1] < N[1]; n[1]++)
-      for (n[2] = 0; n[2] < N[2]; n[2]++) {
+  for (n[0] = 0; n[0] <= N[0]; n[0]++)
+    for (n[1] = 0; n[1] <= N[1]; n[1]++)
+      for (n[2] = 0; n[2] <= N[2]; n[2]++) {
         // calculate position of current grid vector in untransformed box
         CurrentPosition.Init((double)n[0]/(double)N[0], (double)n[1]/(double)N[1], (double)n[2]/(double)N[2]);
         CurrentPosition.MatrixMultiplication(M);
-        Log() << Verbose(2) << "INFO: Current Position is " << CurrentPosition << "." << endl;
-        // Check whether point is in- or outside
-        FillIt = true;
-        i=0;
-        for (MoleculeList::iterator ListRunner = List->ListOfMolecules.begin(); ListRunner != List->ListOfMolecules.end(); ListRunner++) {
-          // get linked cell list
-          if (TesselStruct[i] == NULL) {
-            eLog() << Verbose(0) << "TesselStruct of " << (*ListRunner) << " is NULL. Didn't we pre-create it?" << endl;
-            FillIt = false;
-          } else {
-            const double distance = (TesselStruct[i]->GetDistanceSquaredToSurface(CurrentPosition, LCList[i]));
-            FillIt = FillIt && (distance > boundary*boundary) && ((MaxDistance < 0) || (MaxDistance*MaxDistance > distance));
-            if (FillIt) {
-              Log() << Verbose(1) << "INFO: Position at " << CurrentPosition << " is outer point." << endl;
+        // create molecule random translation vector ...
+        for (int i=0;i<NDIM;i++)
+          FillerTranslations.x[i] = RandomMolDisplacement*(rand()/(RAND_MAX/2.) - 1.);
+        Log() << Verbose(2) << "INFO: Current Position is " << CurrentPosition << "+" << FillerTranslations << "." << endl;
+
+        // go through all atoms
+        Walker = filler->start;
+        while (Walker->next != filler->end) {
+          Walker = Walker->next;
+
+          // create atomic random translation vector ...
+          for (int i=0;i<NDIM;i++)
+            AtomTranslations.x[i] = RandomAtomDisplacement*(rand()/(RAND_MAX/2.) - 1.);
+
+          // ... and rotation matrix
+          if (DoRandomRotation) {
+            for (int i=0;i<NDIM;i++) {
+              phi[i] = rand()/(RAND_MAX/(2.*M_PI));
+            }
+
+            Rotations[0] =   cos(phi[0])            *cos(phi[2]) + (sin(phi[0])*sin(phi[1])*sin(phi[2]));
+            Rotations[3] =   sin(phi[0])            *cos(phi[2]) - (cos(phi[0])*sin(phi[1])*sin(phi[2]));
+            Rotations[6] =               cos(phi[1])*sin(phi[2])                                     ;
+            Rotations[1] = - sin(phi[0])*cos(phi[1])                                                ;
+            Rotations[4] =   cos(phi[0])*cos(phi[1])                                                ;
+            Rotations[7] =               sin(phi[1])                                                ;
+            Rotations[3] = - cos(phi[0])            *sin(phi[2]) + (sin(phi[0])*sin(phi[1])*cos(phi[2]));
+            Rotations[5] = - sin(phi[0])            *sin(phi[2]) - (cos(phi[0])*sin(phi[1])*cos(phi[2]));
+            Rotations[8] =               cos(phi[1])*cos(phi[2])                                     ;
+          }
+
+          // ... and put at new position
+          Inserter.CopyVector(&(Walker->x));
+          if (DoRandomRotation)
+            Inserter.MatrixMultiplication(Rotations);
+          Inserter.AddVector(&AtomTranslations);
+          Inserter.AddVector(&FillerTranslations);
+          Inserter.AddVector(&CurrentPosition);
+
+          // check whether inserter is inside box
+          Inserter.MatrixMultiplication(MInverse);
+          FillIt = true;
+          for (int i=0;i<NDIM;i++)
+            FillIt = FillIt && (Inserter.x[i] >= -MYEPSILON) && ((Inserter.x[i]-1.) <= MYEPSILON);
+          Inserter.MatrixMultiplication(M);
+
+          // Check whether point is in- or outside
+          for (MoleculeList::iterator ListRunner = List->ListOfMolecules.begin(); ListRunner != List->ListOfMolecules.end(); ListRunner++) {
+            // get linked cell list
+            if (TesselStruct[(*ListRunner)] == NULL) {
+              eLog() << Verbose(0) << "TesselStruct of " << (*ListRunner) << " is NULL. Didn't we pre-create it?" << endl;
+              FillIt = false;
             } else {
-              Log() << Verbose(1) << "INFO: Position at " << CurrentPosition << " is inner point, within boundary or outside of MaxDistance." << endl;
-              break;
+              const double distance = (TesselStruct[(*ListRunner)]->GetDistanceSquaredToSurface(Inserter, LCList[(*ListRunner)]));
+              FillIt = FillIt && (distance > boundary*boundary) && ((MaxDistance < 0) || (MaxDistance*MaxDistance > distance));
             }
-            i++;
           }
-        }
-
-        if (FillIt) {
-          // fill in Filler
-          Log() << Verbose(2) << "Space at " << CurrentPosition << " is unoccupied by any molecule, filling in." << endl;
-
-          // create molecule random translation vector ...
-          for (int i=0;i<NDIM;i++)
-            FillerTranslations.x[i] = RandomMolDisplacement*(rand()/(RAND_MAX/2.) - 1.);
-          Log() << Verbose(2) << "INFO: Translating this filler by " << FillerTranslations << "." << endl;
-
-          // go through all atoms
-          Walker = filler->start;
-          while (Walker->next != filler->end) {
-            Walker = Walker->next;
+          // insert into Filling
+          if (FillIt) {
+            Log() << Verbose(1) << "INFO: Position at " << Inserter << " is outer point." << endl;
             // copy atom ...
             CopyAtoms[Walker->nr] = new atom(Walker);
-
-            // create atomic random translation vector ...
-            for (int i=0;i<NDIM;i++)
-              AtomTranslations.x[i] = RandomAtomDisplacement*(rand()/(RAND_MAX/2.) - 1.);
-
-            // ... and rotation matrix
-            if (DoRandomRotation) {
-              for (int i=0;i<NDIM;i++) {
-                phi[i] = rand()/(RAND_MAX/(2.*M_PI));
-              }
-
-              Rotations[0] =   cos(phi[0])            *cos(phi[2]) + (sin(phi[0])*sin(phi[1])*sin(phi[2]));
-              Rotations[3] =   sin(phi[0])            *cos(phi[2]) - (cos(phi[0])*sin(phi[1])*sin(phi[2]));
-              Rotations[6] =               cos(phi[1])*sin(phi[2])                                     ;
-              Rotations[1] = - sin(phi[0])*cos(phi[1])                                                ;
-              Rotations[4] =   cos(phi[0])*cos(phi[1])                                                ;
-              Rotations[7] =               sin(phi[1])                                                ;
-              Rotations[3] = - cos(phi[0])            *sin(phi[2]) + (sin(phi[0])*sin(phi[1])*cos(phi[2]));
-              Rotations[5] = - sin(phi[0])            *sin(phi[2]) - (cos(phi[0])*sin(phi[1])*cos(phi[2]));
-              Rotations[8] =               cos(phi[1])*cos(phi[2])                                     ;
-            }
-
-            // ... and put at new position
-            if (DoRandomRotation)
-              CopyAtoms[Walker->nr]->x.MatrixMultiplication(Rotations);
-            CopyAtoms[Walker->nr]->x.AddVector(&AtomTranslations);
-            CopyAtoms[Walker->nr]->x.AddVector(&FillerTranslations);
-            CopyAtoms[Walker->nr]->x.AddVector(&CurrentPosition);
-
-            // insert into Filling
-
-            // FIXME: gives completely different results if CopyAtoms[..] used instead of Walker, why???
+            CopyAtoms[Walker->nr]->x.CopyVector(&Inserter);
+            Filling->AddAtom(CopyAtoms[Walker->nr]);
             Log() << Verbose(4) << "Filling atom " << *Walker << ", translated to " << AtomTranslations << ", at final position is " << (CopyAtoms[Walker->nr]->x) << "." << endl;
-            Filling->AddAtom(CopyAtoms[Walker->nr]);
+          } else {
+            Log() << Verbose(1) << "INFO: Position at " << Inserter << " is inner point, within boundary or outside of MaxDistance." << endl;
+            CopyAtoms[Walker->nr] = NULL;
+            break;
           }
 
@@ -929,22 +926,14 @@
           while(Binder->next != filler->last) {
             Binder = Binder->next;
-            Log() << Verbose(3) << "Adding Bond between " << *CopyAtoms[Binder->leftatom->nr] << " and " << *CopyAtoms[Binder->rightatom->nr]<< "." << endl;
-            Filling->AddBond(CopyAtoms[Binder->leftatom->nr], CopyAtoms[Binder->rightatom->nr], Binder->BondDegree);
+            if ((CopyAtoms[Binder->leftatom->nr] != NULL) && (CopyAtoms[Binder->rightatom->nr] != NULL)) {
+              Log() << Verbose(3) << "Adding Bond between " << *CopyAtoms[Binder->leftatom->nr] << " and " << *CopyAtoms[Binder->rightatom->nr]<< "." << endl;
+              Filling->AddBond(CopyAtoms[Binder->leftatom->nr], CopyAtoms[Binder->rightatom->nr], Binder->BondDegree);
+            }
           }
-        } else {
-          // leave empty
-          Log() << Verbose(2) << "Space at " << CurrentPosition << " is occupied." << endl;
         }
       }
   Free(&M);
-
-  // output to file
-  TesselStruct[0]->LastTriangle = NULL;
-  StoreTrianglesinFile(Filling, TesselStruct[0], "Tesselated", ".dat");
-
-  for (size_t i=0;i<List->ListOfMolecules.size();i++) {
-  	delete(LCList[i]);
-	delete(TesselStruct[i]);
-  }
+  Free(&MInverse);
+
   return Filling;
 };
