Index: /code/branches/gui/src/core/Loader.cc
===================================================================
--- /code/branches/gui/src/core/Loader.cc	(revision 1645)
+++ /code/branches/gui/src/core/Loader.cc	(revision 1646)
@@ -64,4 +64,6 @@
     void Loader::add(const Level* level, const ClassTreeMask& mask)
     {
+        if (!level)
+            return;
         Loader::levels_s.insert(Loader::levels_s.end(), std::pair<const Level*, ClassTreeMask>(level, mask));
     }
@@ -69,4 +71,6 @@
     void Loader::remove(const Level* level)
     {
+        if (!level)
+            return;
         for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it)
         {
@@ -108,4 +112,7 @@
     bool Loader::load(const Level* level, const ClassTreeMask& mask)
     {
+        if (!level)
+            return false;
+
         Loader::currentMask_s = level->getMask() * mask;
 
@@ -165,4 +172,6 @@
     void Loader::unload(const Level* level, const ClassTreeMask& mask)
     {
+        if (!level)
+            return;
         for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; )
         {
Index: /code/branches/gui/src/core/input/ExtendedInputState.h
===================================================================
--- /code/branches/gui/src/core/input/ExtendedInputState.h	(revision 1645)
+++ /code/branches/gui/src/core/input/ExtendedInputState.h	(revision 1646)
@@ -47,8 +47,8 @@
     class _CoreExport ExtendedInputState : public InputState
     {
+        friend class InputManager;
+        friend class ClassFactory<ExtendedInputState>;
+
     public:
-        ExtendedInputState();
-        ~ExtendedInputState() { }
-
         bool addKeyHandler        (KeyHandler* handler);
         bool removeKeyHandler     (KeyHandler* handler);
@@ -68,4 +68,7 @@
 
     private:
+        ExtendedInputState();
+        ~ExtendedInputState() { }
+
         void tickInput(float dt);
         void tickInput(float dt, unsigned int device);
Index: /code/branches/gui/src/core/input/InputManager.cc
===================================================================
--- /code/branches/gui/src/core/input/InputManager.cc	(revision 1645)
+++ /code/branches/gui/src/core/input/InputManager.cc	(revision 1646)
@@ -429,6 +429,6 @@
                     (*rit).second->onLeave();
                 }
-                activeStates_.clear();
-                _updateActiveStates();
+                //activeStates_.clear();
+                //_updateActiveStates();
 
                 // destroy all input states
@@ -438,7 +438,7 @@
                 }
 
-                stateEmpty_ = 0;
-                stateCalibrator_ = 0;
-                stateDetector_ = 0;
+                //stateEmpty_ = 0;
+                //stateCalibrator_ = 0;
+                //stateDetector_ = 0;
 
                 // destroy the devices
@@ -448,8 +448,8 @@
 
                 // 0 joy sticks now
-                _redimensionLists();
+                //_redimensionLists();
 
                 OIS::InputManager::destroyInputSystem(inputSystem_);
-                inputSystem_ = 0;
+                //inputSystem_ = 0;
 
                 CCOUT(3) << "Destroying done." << std::endl;
@@ -509,4 +509,10 @@
     {
         assert(state);
+        std::map<int, InputState*>::iterator it = this->activeStates_.find(state->getPriority());
+        if (it != this->activeStates_.end())
+        {
+            this->activeStates_.erase(it);
+            _updateActiveStates();
+        }
         inputStatesByPriority_.erase(state->getPriority());
         inputStatesByName_.erase(state->getName());
Index: /code/branches/gui/src/core/input/InputState.h
===================================================================
--- /code/branches/gui/src/core/input/InputState.h	(revision 1645)
+++ /code/branches/gui/src/core/input/InputState.h	(revision 1646)
@@ -51,8 +51,4 @@
 
     public:
-        InputState() : priority_(0), executorOnEnter_(0), executorOnLeave_(0)
-        { RegisterObject(InputState); }
-        virtual ~InputState() { }
-
         const std::string& getName() const { return name_; }
         int getPriority()            const { return priority_; }
@@ -93,4 +89,8 @@
 
     protected:
+        InputState() : priority_(0), executorOnEnter_(0), executorOnLeave_(0)
+        { RegisterObject(InputState); }
+        virtual ~InputState() { }
+
         virtual void numberOfJoySticksChanged(unsigned int n) = 0;
         void setInputDeviceEnabled(unsigned int device, bool bEnabled)
Index: /code/branches/gui/src/core/input/SimpleInputState.h
===================================================================
--- /code/branches/gui/src/core/input/SimpleInputState.h	(revision 1645)
+++ /code/branches/gui/src/core/input/SimpleInputState.h	(revision 1646)
@@ -45,8 +45,8 @@
     class _CoreExport SimpleInputState : public InputState
     {
+        friend class InputManager;
+        friend class ClassFactory<SimpleInputState>;
+
     public:
-        SimpleInputState();
-        ~SimpleInputState() { }
-
         void setKeyHandler        (KeyHandler* handler) { keyHandler_ = handler; update(); }
         void setMouseHandler      (MouseHandler* handler) { mouseHandler_ = handler; update(); }
@@ -57,4 +57,7 @@
 
     private:
+        SimpleInputState();
+        ~SimpleInputState() { }
+
         void tickInput(float dt);
         void tickInput(float dt, unsigned int device);
Index: /code/branches/gui/src/orxonox/Orxonox.cc
===================================================================
--- /code/branches/gui/src/orxonox/Orxonox.cc	(revision 1645)
+++ /code/branches/gui/src/orxonox/Orxonox.cc	(revision 1646)
@@ -115,4 +115,6 @@
     , inputManager_(0)
     , radar_(0)
+    , console_(0)
+    , guiManager_(0)
   {
     RegisterRootObject(Orxonox);
@@ -142,4 +144,7 @@
     if (this->radar_)
       delete this->radar_;
+
+    if (this->guiManager_)
+      delete guiManager_;
 
     //if (this->auMan_)
@@ -271,5 +276,6 @@
 
           // load the CEGUI interface
-          GUIManager::getInstance().initialise();
+          guiManager_ = new GUIManager();
+          guiManager_->initialise();
         }
 
Index: /code/branches/gui/src/orxonox/Orxonox.h
===================================================================
--- /code/branches/gui/src/orxonox/Orxonox.h	(revision 1645)
+++ /code/branches/gui/src/orxonox/Orxonox.h	(revision 1646)
@@ -103,4 +103,5 @@
       Radar*                radar_;         //!< represents the Radar (not the HUD part)
       InGameConsole*        console_;
+      GUIManager*           guiManager_;
 
       static Orxonox *singletonRef_s;
Index: /code/branches/gui/src/orxonox/OrxonoxPrereqs.h
===================================================================
--- /code/branches/gui/src/orxonox/OrxonoxPrereqs.h	(revision 1645)
+++ /code/branches/gui/src/orxonox/OrxonoxPrereqs.h	(revision 1646)
@@ -118,4 +118,7 @@
     class OverlayGroup;
     class OverlayText;
+
+    //gui
+    class GUIManager;
 }
 
Index: /code/branches/gui/src/orxonox/gui/GUIManager.cc
===================================================================
--- /code/branches/gui/src/orxonox/gui/GUIManager.cc	(revision 1645)
+++ /code/branches/gui/src/orxonox/gui/GUIManager.cc	(revision 1646)
@@ -58,4 +58,6 @@
     SetConsoleCommandShortcut(GUIManager, showGUI_s).setKeybindMode(KeybindMode::OnPress);
 
+    GUIManager* GUIManager::singletonRef_s = 0;
+
     GUIManager::GUIManager()
         //: emptySceneManager_(0)
@@ -71,9 +73,39 @@
         , state_(Uninitialised)
     {
+        assert(singletonRef_s == 0);
+        singletonRef_s = this;
     }
 
     GUIManager::~GUIManager()
     {
-        // TODO: destruct at least something
+        if (backgroundCamera_)
+            backgroundSceneManager_->destroyCamera(backgroundCamera_);
+
+        if (backgroundSceneManager_)
+            Ogre::Root::getSingleton().destroySceneManager(backgroundSceneManager_);
+
+        InputManager::getInstance().destroyState("gui");
+
+        if (guiSystem_)
+            delete guiSystem_;
+
+        if (scriptModule_)
+        {
+            // destroy our own tolua interfaces
+	        //lua_pushnil(luaState_);
+	        //lua_setglobal(luaState_, "Orxonox");
+	        //lua_pushnil(luaState_);
+	        //lua_setglobal(luaState_, "Core");
+            // TODO: deleting the script module fails an assertation.
+            // However there is not much we can do about it since it occurs too when
+            // we don't open Core or Orxonox. Might be a CEGUI issue.
+            // The memory leak is not a problem anyway..
+            //delete scriptModule_;
+        }
+
+        if (guiRenderer_)
+            delete guiRenderer_;
+
+        singletonRef_s = 0;
     }
 
@@ -104,4 +136,5 @@
                 // setup scripting
                 this->scriptModule_ = new LuaScriptModule();
+                this->luaState_ = this->scriptModule_->getLuaState();
 
                 // create the CEGUI system singleton
@@ -267,16 +300,4 @@
 
 
-    /**
-    @brief
-        Returns a unique instance of GUIManager.
-    @return
-        The instance
-    */
-    GUIManager& GUIManager::getInstance()
-    {
-        static GUIManager instance;
-        return instance;
-    }
-
     inline CEGUI::MouseButton GUIManager::convertButton(MouseButton::Enum button)
     {
Index: /code/branches/gui/src/orxonox/gui/GUIManager.h
===================================================================
--- /code/branches/gui/src/orxonox/gui/GUIManager.h	(revision 1645)
+++ /code/branches/gui/src/orxonox/gui/GUIManager.h	(revision 1646)
@@ -48,4 +48,5 @@
     class LuaScriptModule;
 }
+struct lua_State;
 
 namespace orxonox // tolua_export
@@ -68,4 +69,7 @@
         };
 
+        GUIManager();
+        ~GUIManager();
+
         bool initialise();
         void tick(float dt);
@@ -75,5 +79,4 @@
         Ogre::Camera* getCamera() { return this->backgroundCamera_; }
 
-        static GUIManager& getInstance(); // tolua_export
         static void showGUI_s(const std::string& name, Ogre::SceneManager* sceneManager)//bool showBackground)
         {
@@ -81,8 +84,9 @@
         }
 
+        static GUIManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; } // tolua_export
+        static GUIManager* getInstancePtr() { return singletonRef_s; }
+
     private:
-        GUIManager();
         GUIManager(const GUIManager& instance);
-        ~GUIManager();
 
         void keyPressed (const KeyEvent& evt)
@@ -119,9 +123,11 @@
         CEGUI::System*            guiSystem_;
         CEGUI::Imageset*          backgroundImage_;
+        lua_State*                luaState_;
 
         State state_;
 
+        static CEGUI::MouseButton convertButton(MouseButton::Enum button);
 
-        static CEGUI::MouseButton convertButton(MouseButton::Enum button);
+        static GUIManager*        singletonRef_s;
     }; // tolua_export
 
Index: /code/branches/gui/visual_studio/core_properties.vsprops
===================================================================
--- /code/branches/gui/visual_studio/core_properties.vsprops	(revision 1645)
+++ /code/branches/gui/visual_studio/core_properties.vsprops	(revision 1646)
@@ -13,5 +13,5 @@
 	<Tool
 		Name="VCLinkerTool"
-		AdditionalDependencies="OgreMain$(CSS).lib lua-5.1.3$(CS).lib tcl85t.lib"
+		AdditionalDependencies="OgreMain$(CSS).lib tcl85t.lib lua-5.1.3$(CS).lib"
 	/>
 </VisualStudioPropertySheet>
Index: /code/branches/gui/visual_studio/orxonox_vc8.sln
===================================================================
--- /code/branches/gui/visual_studio/orxonox_vc8.sln	(revision 1645)
+++ /code/branches/gui/visual_studio/orxonox_vc8.sln	(revision 1646)
@@ -10,8 +10,7 @@
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tolua", "vc8\tolua.vcproj", "{35E36A06-0A5C-4A0D-9AB6-5A05EAA87626}"
 EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "audio", "vc8\audio.vcproj", "{4733BD1A-E04C-458D-8BFB-5010250EA497}"
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tolua_gen", "vc8\tolua_gen.vcproj", "{71FC0211-5EB5-4637-BE8A-A48EC3CC27D0}"
 	ProjectSection(ProjectDependencies) = postProject
-		{271715F3-5B90-4110-A552-70C788084A86} = {271715F3-5B90-4110-A552-70C788084A86}
-		{2240ECD7-2F48-4431-8E1B-25466A384CCC} = {2240ECD7-2F48-4431-8E1B-25466A384CCC}
+		{35E36A06-0A5C-4A0D-9AB6-5A05EAA87626} = {35E36A06-0A5C-4A0D-9AB6-5A05EAA87626}
 	EndProjectSection
 EndProject
@@ -23,22 +22,24 @@
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "util", "vc8\util.vcproj", "{2240ECD7-2F48-4431-8E1B-25466A384CCC}"
-	ProjectSection(ProjectDependencies) = postProject
-		{F101C2F0-1CB9-4A57-827B-6C399A99B28F} = {F101C2F0-1CB9-4A57-827B-6C399A99B28F}
-	EndProjectSection
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "core", "vc8\core.vcproj", "{271715F3-5B90-4110-A552-70C788084A86}"
 	ProjectSection(ProjectDependencies) = postProject
+		{F101C2F0-1CB9-4A57-827B-6C399A99B28F} = {F101C2F0-1CB9-4A57-827B-6C399A99B28F}
+		{2240ECD7-2F48-4431-8E1B-25466A384CCC} = {2240ECD7-2F48-4431-8E1B-25466A384CCC}
+		{9CC704CB-4956-4479-BDEC-57CBC03F700E} = {9CC704CB-4956-4479-BDEC-57CBC03F700E}
+		{53C56131-E2AA-4A27-B460-7AC05D61A0E6} = {53C56131-E2AA-4A27-B460-7AC05D61A0E6}
 		{35E36A06-0A5C-4A0D-9AB6-5A05EAA87626} = {35E36A06-0A5C-4A0D-9AB6-5A05EAA87626}
-		{53C56131-E2AA-4A27-B460-7AC05D61A0E6} = {53C56131-E2AA-4A27-B460-7AC05D61A0E6}
-		{A0724246-CB7C-420B-BCF0-68EF205AFE34} = {A0724246-CB7C-420B-BCF0-68EF205AFE34}
-		{9CC704CB-4956-4479-BDEC-57CBC03F700E} = {9CC704CB-4956-4479-BDEC-57CBC03F700E}
+	EndProjectSection
+EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "audio", "vc8\audio.vcproj", "{4733BD1A-E04C-458D-8BFB-5010250EA497}"
+	ProjectSection(ProjectDependencies) = postProject
+		{271715F3-5B90-4110-A552-70C788084A86} = {271715F3-5B90-4110-A552-70C788084A86}
 		{2240ECD7-2F48-4431-8E1B-25466A384CCC} = {2240ECD7-2F48-4431-8E1B-25466A384CCC}
-		{F101C2F0-1CB9-4A57-827B-6C399A99B28F} = {F101C2F0-1CB9-4A57-827B-6C399A99B28F}
 	EndProjectSection
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "network", "vc8\network.vcproj", "{35575B59-E1AE-40E8-89C4-2862B5B09B68}"
 	ProjectSection(ProjectDependencies) = postProject
+		{271715F3-5B90-4110-A552-70C788084A86} = {271715F3-5B90-4110-A552-70C788084A86}
 		{2240ECD7-2F48-4431-8E1B-25466A384CCC} = {2240ECD7-2F48-4431-8E1B-25466A384CCC}
-		{271715F3-5B90-4110-A552-70C788084A86} = {271715F3-5B90-4110-A552-70C788084A86}
 	EndProjectSection
 EndProject
@@ -46,14 +47,9 @@
 	ProjectSection(ProjectDependencies) = postProject
 		{35E36A06-0A5C-4A0D-9AB6-5A05EAA87626} = {35E36A06-0A5C-4A0D-9AB6-5A05EAA87626}
-		{35575B59-E1AE-40E8-89C4-2862B5B09B68} = {35575B59-E1AE-40E8-89C4-2862B5B09B68}
-		{2240ECD7-2F48-4431-8E1B-25466A384CCC} = {2240ECD7-2F48-4431-8E1B-25466A384CCC}
 		{F101C2F0-1CB9-4A57-827B-6C399A99B28F} = {F101C2F0-1CB9-4A57-827B-6C399A99B28F}
 		{271715F3-5B90-4110-A552-70C788084A86} = {271715F3-5B90-4110-A552-70C788084A86}
+		{2240ECD7-2F48-4431-8E1B-25466A384CCC} = {2240ECD7-2F48-4431-8E1B-25466A384CCC}
+		{35575B59-E1AE-40E8-89C4-2862B5B09B68} = {35575B59-E1AE-40E8-89C4-2862B5B09B68}
 		{4733BD1A-E04C-458D-8BFB-5010250EA497} = {4733BD1A-E04C-458D-8BFB-5010250EA497}
-	EndProjectSection
-EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tolua_gen", "vc8\tolua_gen.vcproj", "{71FC0211-5EB5-4637-BE8A-A48EC3CC27D0}"
-	ProjectSection(ProjectDependencies) = postProject
-		{35E36A06-0A5C-4A0D-9AB6-5A05EAA87626} = {35E36A06-0A5C-4A0D-9AB6-5A05EAA87626}
 	EndProjectSection
 EndProject
@@ -80,8 +76,8 @@
 		{35E36A06-0A5C-4A0D-9AB6-5A05EAA87626}.Release|Win32.ActiveCfg = Release|Win32
 		{35E36A06-0A5C-4A0D-9AB6-5A05EAA87626}.Release|Win32.Build.0 = Release|Win32
-		{4733BD1A-E04C-458D-8BFB-5010250EA497}.Debug|Win32.ActiveCfg = Debug|Win32
-		{4733BD1A-E04C-458D-8BFB-5010250EA497}.Debug|Win32.Build.0 = Debug|Win32
-		{4733BD1A-E04C-458D-8BFB-5010250EA497}.Release|Win32.ActiveCfg = Release|Win32
-		{4733BD1A-E04C-458D-8BFB-5010250EA497}.Release|Win32.Build.0 = Release|Win32
+		{71FC0211-5EB5-4637-BE8A-A48EC3CC27D0}.Debug|Win32.ActiveCfg = Debug|Win32
+		{71FC0211-5EB5-4637-BE8A-A48EC3CC27D0}.Debug|Win32.Build.0 = Debug|Win32
+		{71FC0211-5EB5-4637-BE8A-A48EC3CC27D0}.Release|Win32.ActiveCfg = Release|Win32
+		{71FC0211-5EB5-4637-BE8A-A48EC3CC27D0}.Release|Win32.Build.0 = Release|Win32
 		{A0724246-CB7C-420B-BCF0-68EF205AFE34}.Debug|Win32.ActiveCfg = Debug|Win32
 		{A0724246-CB7C-420B-BCF0-68EF205AFE34}.Debug|Win32.Build.0 = Debug|Win32
@@ -96,4 +92,8 @@
 		{271715F3-5B90-4110-A552-70C788084A86}.Release|Win32.ActiveCfg = Release|Win32
 		{271715F3-5B90-4110-A552-70C788084A86}.Release|Win32.Build.0 = Release|Win32
+		{4733BD1A-E04C-458D-8BFB-5010250EA497}.Debug|Win32.ActiveCfg = Debug|Win32
+		{4733BD1A-E04C-458D-8BFB-5010250EA497}.Debug|Win32.Build.0 = Debug|Win32
+		{4733BD1A-E04C-458D-8BFB-5010250EA497}.Release|Win32.ActiveCfg = Release|Win32
+		{4733BD1A-E04C-458D-8BFB-5010250EA497}.Release|Win32.Build.0 = Release|Win32
 		{35575B59-E1AE-40E8-89C4-2862B5B09B68}.Debug|Win32.ActiveCfg = Debug|Win32
 		{35575B59-E1AE-40E8-89C4-2862B5B09B68}.Debug|Win32.Build.0 = Debug|Win32
@@ -104,8 +104,4 @@
 		{0B6C5CFD-F91B-432A-80A3-2610F61E060B}.Release|Win32.ActiveCfg = Release|Win32
 		{0B6C5CFD-F91B-432A-80A3-2610F61E060B}.Release|Win32.Build.0 = Release|Win32
-		{71FC0211-5EB5-4637-BE8A-A48EC3CC27D0}.Debug|Win32.ActiveCfg = Debug|Win32
-		{71FC0211-5EB5-4637-BE8A-A48EC3CC27D0}.Debug|Win32.Build.0 = Debug|Win32
-		{71FC0211-5EB5-4637-BE8A-A48EC3CC27D0}.Release|Win32.ActiveCfg = Release|Win32
-		{71FC0211-5EB5-4637-BE8A-A48EC3CC27D0}.Release|Win32.Build.0 = Release|Win32
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
Index: /code/branches/gui/visual_studio/util_properties.vsprops
===================================================================
--- /code/branches/gui/visual_studio/util_properties.vsprops	(revision 1645)
+++ /code/branches/gui/visual_studio/util_properties.vsprops	(revision 1646)
@@ -8,4 +8,5 @@
 	<Tool
 		Name="VCCLCompilerTool"
+		AdditionalOptions="/MP2"
 		PreprocessorDefinitions="UTIL_SHARED_BUILD"
 	/>
