Index: src/World.cpp
===================================================================
--- src/World.cpp	(revision 7a1ce5357081767198c4e7f7e867143f5d19dda0)
+++ src/World.cpp	(revision 3548592787dc5efb58b2018c79af1646956f3fc3)
@@ -9,4 +9,6 @@
 
 #include "atom.hpp"
+#include "molecule.hpp"
+#include "periodentafel.hpp"
 #include "Descriptors/AtomDescriptor.hpp"
 
@@ -14,5 +16,5 @@
 
 /******************************* getter and setter ************************/
-periodentafel* World::getPeriode(){
+periodentafel *&World::getPeriode(){
   return periode;
 }
@@ -26,4 +28,23 @@
 }
 
+int World::numAtoms(){
+  return atoms.size();
+}
+
+int World::numMolecules(){
+  return molecules_deprecated->ListOfMolecules.size();
+}
+
+molecule* World::createMolecule(){
+  OBSERVE;
+  molecule *mol = NULL;
+  mol = new molecule(periode);
+  molecules_deprecated->insert(mol);
+  molecules.insert(mol);
+  mol->signOn(this);
+  return mol;
+}
+
+
 /******************************* Singleton Stuff **************************/
 
@@ -35,9 +56,13 @@
 
 World::World() :
-    dummyId(0)
+    dummyId(0),
+    periode(new periodentafel),
+    molecules_deprecated(new MoleculeListClass)
 {}
 
 World::~World()
-{}
+{
+  delete periode;
+}
 
 World* World::get(){
@@ -51,4 +76,8 @@
 
 void World::destroy(){
+  // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise
+  theWorld->destroyLegacy();
+  //WARNING: at this point we have a small race condition, when sombody now tries to access the world.
+
   // boost supports RAII-Style locking, so we don't need to unlock
   boost::mutex::scoped_lock guard(worldLock);
@@ -58,4 +87,8 @@
 
 World* World::reset(){
+  // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise
+  theWorld->destroyLegacy();
+  //WARNING: at this point we have a small race condition, when sombody now tries to access the world.
+
   World* oldWorld = 0;
   {
@@ -81,6 +114,6 @@
 /******************************* deprecated Legacy Stuff ***********************/
 
-MoleculeListClass *World::getMolecules() {
-  return molecules;
+MoleculeListClass *&World::getMolecules() {
+  return molecules_deprecated;
 }
 
@@ -91,4 +124,8 @@
 }
 
+void World::destroyLegacy(){
+  //delete molecules_deprecated;
+}
+
 void World::unregisterAtom(atom *theAtom){
   OBSERVE;
Index: src/World.hpp
===================================================================
--- src/World.hpp	(revision 7a1ce5357081767198c4e7f7e867143f5d19dda0)
+++ src/World.hpp	(revision 3548592787dc5efb58b2018c79af1646956f3fc3)
@@ -12,4 +12,5 @@
 #include <map>
 #include <vector>
+#include <set>
 
 #include "Patterns/Observer.hpp"
@@ -20,4 +21,5 @@
 class MoleculeListClass;
 class atom;
+class molecule;
 class AtomDescriptor;
 class AtomDescriptor_impl;
@@ -29,10 +31,17 @@
 
   /***** getter and setter *****/
-  periodentafel* getPeriode();
+  // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
+  periodentafel *&getPeriode();
   atom* getAtom(AtomDescriptor descriptor);
   std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
+  int numAtoms();
+  int numMolecules();
+
+  /***** Methods to work with the World *****/
+  molecule *createMolecule();
 private:
   periodentafel *periode;
   std::map<int,atom*> atoms;
+  std::set<molecule*> molecules;
 
 
@@ -57,5 +66,5 @@
    *****/
 public:
-  MoleculeListClass *getMolecules();
+  MoleculeListClass *&getMolecules();
 
   // functions used for the WorldContent template mechanism
@@ -63,5 +72,10 @@
   void unregisterAtom(atom *theAtom);
 private:
-  MoleculeListClass *molecules;
+  // this function cleans up anything that cannot be cleaned while the lock is active
+  // at a later point all these cleanups have to be moved to the World Class so the deadlock and
+  // race condition can both be avoided.
+  void destroyLegacy();
+
+  MoleculeListClass *molecules_deprecated;
 
   // this is needed to assign unique IDs to atoms... so far
Index: src/builder.cpp
===================================================================
--- src/builder.cpp	(revision 7a1ce5357081767198c4e7f7e867143f5d19dda0)
+++ src/builder.cpp	(revision 3548592787dc5efb58b2018c79af1646956f3fc3)
@@ -76,4 +76,5 @@
 #include "Actions/MethodAction.hpp"
 #include "Actions/small_actions.hpp"
+#include "World.hpp"
 #include "version.h"
 
@@ -2164,8 +2165,20 @@
 /********************************************** Main routine **************************************/
 
+void cleanUp(config *configuration){
+  World::destroy();
+  delete(configuration);
+  Log() << Verbose(0) <<  "Maximum of allocated memory: "
+    << MemoryUsageObserver::getInstance()->getMaximumUsedMemory() << endl;
+  Log() << Verbose(0) <<  "Remaining non-freed memory: "
+    << MemoryUsageObserver::getInstance()->getUsedMemorySize() << endl;
+  MemoryUsageObserver::purgeInstance();
+  logger::purgeInstance();
+  errorLogger::purgeInstance();
+  UIFactory::purgeInstance();
+  ActionRegistry::purgeRegistry();
+}
+
 int main(int argc, char **argv)
 {
-  periodentafel *periode = new periodentafel;
-    MoleculeListClass *molecules = new MoleculeListClass;
     molecule *mol = NULL;
     config *configuration = new config;
@@ -2178,23 +2191,16 @@
     setVerbosity(0);
     /* structure of ParseCommandLineOptions will be refactored later */
-    j = ParseCommandLineOptions(argc, argv, molecules, periode, *configuration, ConfigFileName);
+    j = ParseCommandLineOptions(argc, argv,  World::get()->getMolecules(), World::get()->getPeriode(), *configuration, ConfigFileName);
     switch (j){
         case 255:
         case 2:
         case 1:
-            delete (molecules);
-            delete (periode);
-            delete (configuration);
-            Log() << Verbose(0) << "Maximum of allocated memory: " << MemoryUsageObserver::getInstance()->getMaximumUsedMemory() << endl;
-            Log() << Verbose(0) << "Remaining non-freed memory: " << MemoryUsageObserver::getInstance()->getUsedMemorySize() << endl;
-            MemoryUsageObserver::getInstance()->purgeInstance();
-            logger::purgeInstance();
-            errorLogger::purgeInstance();
+            cleanUp(configuration);
             return (j == 1 ? 0 : j);
         default:
             break;
     }
-    if(molecules->ListOfMolecules.size() == 0){
-        mol = new molecule(periode);
+    if(World::get()->numMolecules() == 0){
+        mol = World::get()->createMolecule();
         if(mol->cell_size[0] == 0.){
             Log() << Verbose(0) << "enter lower tridiagonal form of basis matrix" << endl << endl;
@@ -2204,7 +2210,5 @@
             }
         }
-
         mol->ActiveFlag = true;
-        molecules->insert(mol);
     }
 
@@ -2218,10 +2222,10 @@
 
       UIFactory::makeUserInterface(UIFactory::Text);
-      MainWindow *mainWindow = UIFactory::get()->makeMainWindow(populaters,molecules, configuration, periode, ConfigFileName);
+      MainWindow *mainWindow = UIFactory::get()->makeMainWindow(populaters,World::get()->getMolecules(), configuration, World::get()->getPeriode(), ConfigFileName);
       mainWindow->display();
       delete mainWindow;
     }
 
-    if(periode->StorePeriodentafel(configuration->databasepath))
+    if(World::get()->getPeriode()->StorePeriodentafel(configuration->databasepath))
         Log() << Verbose(0) << "Saving of elements.db successful." << endl;
 
@@ -2229,19 +2233,5 @@
         Log() << Verbose(0) << "Saving of elements.db failed." << endl;
 
-    delete (molecules);
-    delete(periode);
-  delete(configuration);
-
-
-
-  Log() << Verbose(0) <<  "Maximum of allocated memory: "
-    << MemoryUsageObserver::getInstance()->getMaximumUsedMemory() << endl;
-  Log() << Verbose(0) <<  "Remaining non-freed memory: "
-    << MemoryUsageObserver::getInstance()->getUsedMemorySize() << endl;
-  MemoryUsageObserver::purgeInstance();
-  logger::purgeInstance();
-  errorLogger::purgeInstance();
-  UIFactory::purgeInstance();
-  ActionRegistry::purgeRegistry();
+  cleanUp(configuration);
   return (0);
 }
