Index: code/branches/gui/src/core/CommandLine.h
===================================================================
--- code/branches/gui/src/core/CommandLine.h	(revision 1665)
+++ code/branches/gui/src/core/CommandLine.h	(revision 1670)
@@ -38,8 +38,8 @@
 #define SetCommandLineArgument(name, defaultValue) \
     BaseCommandLineArgument& CmdArgumentDummyBoolVar##name \
-    = orxonox::CommandLine::addCommandLineArgument(#name, defaultValue)
+    = orxonox::CommandLine::addArgument(#name, defaultValue)
 #define SetCommandLineSwitch(name) \
     BaseCommandLineArgument& CmdArgumentDummyBoolVar##name \
-    = orxonox::CommandLine::addCommandLineArgument(#name, false)
+    = orxonox::CommandLine::addArgument(#name, false)
 
 
@@ -89,6 +89,6 @@
     protected:
         BaseCommandLineArgument(const std::string& name)
-            : name_(name)
-            , bHasDefaultValue_(true)
+            : bHasDefaultValue_(true)
+            , name_(name)
         { }
 
@@ -216,11 +216,11 @@
 
         template <class T>
-        static const CommandLineArgument<T>* getCommandLineArgument(const std::string& name);
+        static const CommandLineArgument<T>* getArgument(const std::string& name);
         //! Writes the argument value in the given parameter.
         template <class T>
-        static void getCommandLineValue(const std::string& name, T* value)
-        { *value = getCommandLineArgument<T>(name)->getValue(); }
-        template <class T>
-        static BaseCommandLineArgument& addCommandLineArgument(const std::string& name, T defaultValue);
+        static void getValue(const std::string& name, T* value)
+        { *value = getArgument<T>(name)->getValue(); }
+        template <class T>
+        static BaseCommandLineArgument& addArgument(const std::string& name, T defaultValue);
 
     private:
@@ -264,5 +264,5 @@
     */
     template <class T>
-    const CommandLineArgument<T>* CommandLine::getCommandLineArgument(const std::string& name)
+    const CommandLineArgument<T>* CommandLine::getArgument(const std::string& name)
     {
         std::map<std::string, BaseCommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
@@ -302,5 +302,5 @@
     */
     template <class T>
-    BaseCommandLineArgument& CommandLine::addCommandLineArgument(const std::string& name, T defaultValue)
+    BaseCommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue)
     {
         std::map<std::string, BaseCommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
Index: code/branches/gui/src/core/Core.cc
===================================================================
--- code/branches/gui/src/core/Core.cc	(revision 1665)
+++ code/branches/gui/src/core/Core.cc	(revision 1670)
@@ -36,6 +36,6 @@
 #include "CoreIncludes.h"
 #include "ConfigValueIncludes.h"
-#include "input/InputManager.h"
-#include "TclThreadManager.h"
+//#include "input/InputManager.h"
+//#include "TclThreadManager.h"
 
 namespace orxonox
@@ -188,14 +188,14 @@
     }
 
-    /**
-        @brief Ticks every core class in a specified sequence. Has to be called
-               every Orxonox tick!
-        @param dt Delta Time
-    */
-    void Core::tick(float dt)
-    {
-        TclThreadManager::getInstance().tick(dt);
-        InputManager::getInstance().tick(dt);
-    }
+    ///**
+    //    @brief Ticks every core class in a specified sequence. Has to be called
+    //           every Orxonox tick!
+    //    @param dt Delta Time
+    //*/
+    //void Core::tick(float dt)
+    //{
+    //    TclThreadManager::getInstance().tick(dt);
+    //    InputManager::getInstance().tick(dt);
+    //}
 }
 
Index: code/branches/gui/src/core/Core.h
===================================================================
--- code/branches/gui/src/core/Core.h	(revision 1665)
+++ code/branches/gui/src/core/Core.h	(revision 1670)
@@ -58,5 +58,5 @@
             static void resetLanguage();
 
-            static void tick(float dt);
+            //static void tick(float dt);
 
         private:
Index: code/branches/gui/src/core/Exception.h
===================================================================
--- code/branches/gui/src/core/Exception.h	(revision 1665)
+++ code/branches/gui/src/core/Exception.h	(revision 1670)
@@ -88,5 +88,5 @@
         std::string functionName_;
         std::string fileName_;
-        // mutable because of "what()" is a const method
+        // mutable because "what()" is a const method
         mutable std::string fullDescription_;
     };
@@ -101,6 +101,6 @@
                   : Exception(description, lineNumber, fileName, functionName)
         {
-            // let the catcher decide whether to display the message or not
-            //COUT(1) << this->getFullDescription() << std::endl;
+            // let the catcher decide whether to display the message below level 3
+            COUT(3) << this->getFullDescription() << std::endl;
         }
 
@@ -108,6 +108,6 @@
             : Exception(description)
         {
-            // let the catcher decide whether to display the message or not
-            //COUT(1) << this->getFullDescription() << std::endl;
+            // let the catcher decide whether to display the message below level 3
+            COUT(3) << this->getFullDescription() << std::endl;
         }
 
Index: code/branches/gui/src/core/GameState.cc
===================================================================
--- code/branches/gui/src/core/GameState.cc	(revision 1665)
+++ code/branches/gui/src/core/GameState.cc	(revision 1670)
@@ -47,9 +47,13 @@
         : name_(name)
         , bPauseParent_(false)
-        , bActive_(false)
-        , bSuspended_(false)
+        //, bActive_(false)
+        //, bSuspended_(false)
+        //, bRunning_(false)
+        , scheduledTransition_(0)
         , parent_(0)
         , activeChild_(0)
     {
+        Operations temp = {false, false, false, false, false};
+        this->operation_ = temp;
     }
 
@@ -60,5 +64,5 @@
     GameState::~GameState()
     {
-        if (this->bActive_)
+        if (this->operation_.active)
         {
             if (this->parent_)
@@ -133,5 +137,5 @@
         if (it != this->grandchildrenToChildren_.end())
         {
-            if (state->isActive())
+            if (state->getOperation().active)
             {
                 ThrowException(GameState, "Cannot remove active game state child '"
@@ -247,5 +251,5 @@
     GameState* GameState::getCurrentState()
     {
-        if (this->bActive_)
+        if (this->operation_.active)
         {
             if (this->activeChild_)
@@ -284,21 +288,28 @@
     void GameState::requestState(const std::string& name)
     {
-        if (name == "")
-        {
-            // user would like to leave every state.
-            GameState* current = getCurrentState();
-            if (current)
-            {
-                // Deactivate all states but root
-                GameState* root = getRootNode();
-                current->makeTransition(root);
-                // Kick root too
-                root->deactivate();
-            }
-        }
+        GameState* current = getCurrentState();
+        if (current != 0 && (current->getOperation().entering || current->getOperation().leaving))
+        {
+            ThrowException(GameState, "Making state transitions during enter()/leave() is forbidden.");
+        }
+        //if (name == "")
+        //{
+        //    // user would like to leave every state.
+        //    if (current)
+        //    {
+        //        // Deactivate all states but root
+        //        GameState* root = getRootNode();
+        //        current->makeTransition(root);
+        //        //// Kick root too
+        //        //assert(!(root->getOperation().entering || root->getOperation().leaving));
+        //        //if (root->operation_.running)
+        //        //    root->scheduledTransition_ = 0;
+        //        //else
+        //        //    root->deactivate();
+        //    }
+        //}
         else
         {
             GameState* request = checkState(name);
-            GameState* current = getCurrentState();
             if (request)
             {
@@ -331,5 +342,5 @@
     {
         // we're always already active
-        assert(this->bActive_);
+        assert(this->operation_.active);
 
         if (state == this)
@@ -349,31 +360,68 @@
             assert(this->parent_ != 0);
 
-            // first, leave this state.
+            // only do the transition if we're not currently running
+            if (this->operation_.running)
+            {
+                //this->bDeactivationScheduled_ = true;
+                this->scheduledTransition_ = state;
+            }
+            else
+            {
+                this->deactivate();
+                this->parent_->makeTransition(state);
+            }
+
+        }
+    }
+
+    /**
+    @brief
+        Activates the state. Only sets bActive_ to true and notifies the parent.
+    */
+    void GameState::activate()
+    {
+        if (this->parent_)
+            this->parent_->activeChild_ = this;
+        this->operation_.active = true;
+        this->operation_.entering = true;
+        this->enter();
+        this->operation_.entering = false;
+    }
+
+    /**
+        Activates the state. Only sets bActive_ to false and notifies the parent.
+    */
+    void GameState::deactivate()
+    {
+        this->operation_.leaving = true;
+        this->leave();
+        this->operation_.leaving = false;
+        this->operation_.active = false;
+        if (this->parent_)
+            this->parent_->activeChild_ = 0;
+    }
+
+    /**
+    @brief
+        Update method that calls ticked() with enclosed bRunning_ = true
+        If there was a state transition request within ticked() then this
+        method will transition in the end.
+    @param dt Delta time
+    @note
+        This method is not virtual! You cannot override it therefore.
+    */
+    void GameState::tick(float dt)
+    {
+        this->operation_.running = true;
+        this->ticked(dt);
+        this->operation_.running = false;
+
+        if (this->scheduledTransition_)
+        {
+            // state was requested to be deactivated when ticked.
+            this->makeTransition(this->scheduledTransition_);
+            this->scheduledTransition_ = 0;
             this->deactivate();
-            this->parent_->makeTransition(state);
-        }
-    }
-
-    /**
-    @brief
-        Activates the state. Only sets bActive_ to true and notifies the parent.
-    */
-    void GameState::activate()
-    {
-        this->bActive_ = true;
-        if (this->parent_)
-            this->parent_->activeChild_ = this;
-        this->enter();
-    }
-
-    /**
-        Activates the state. Only sets bActive_ to false and notifies the parent.
-    */
-    void GameState::deactivate()
-    {
-        this->leave();
-        this->bActive_ = false;
-        if (this->parent_)
-            this->parent_->activeChild_ = 0;
+        }
     }
 
Index: code/branches/gui/src/core/GameState.h
===================================================================
--- code/branches/gui/src/core/GameState.h	(revision 1665)
+++ code/branches/gui/src/core/GameState.h	(revision 1670)
@@ -61,4 +61,13 @@
     {
     public:
+        struct Operations
+        {
+            unsigned active    : 1;
+            unsigned entering  : 1;
+            unsigned leaving   : 1;
+            unsigned running   : 1;
+            unsigned suspended : 1;
+        };
+
         GameState(const std::string& name);
         virtual ~GameState();
@@ -71,21 +80,25 @@
         void requestState(const std::string& name);
 
-        //! Determines whether the state is active.
-        bool isActive()       { return this->bActive_; }
-        //! Determines whether the state is suspended.
-        bool isSuspended()    { return this->bSuspended_; }
-        //! Determines whether the state is the current
-        bool isCurrentState() { return this->bActive_ && !this->activeChild_; }
+        ////! Determines whether the state is active.
+        //bool isActive()       { return this->bActive_; }
+        ////! Determines whether the state is suspended.
+        //bool isSuspended()    { return this->bSuspended_; }
+        ////! Determines whether the state is the current
+        //bool isCurrentState() { return this->bActive_ && !this->activeChild_; }
+        const Operations getOperation() { return this->operation_; }
 
-        virtual bool tick(float dt) { return true; }
+        void tick(float dt);
+        void tickChild(float dt) { if (this->activeChild_) this->activeChild_->tick(dt); }
 
     protected:
-        //virtual void enter() = 0;
-        //virtual void leave() = 0;
-        //virtual void tick(float dt) = 0;
-        virtual void enter() { }
-        virtual void leave() { }
+        virtual void enter() = 0;
+        virtual void leave() = 0;
+        virtual void ticked(float dt) = 0;
+        //virtual void enter() { }
+        //virtual void leave() { }
+        //virtual void ticked(float dt) { }
 
         GameState* getActiveChild() { return this->activeChild_; }
+        bool hasScheduledTransition() { return this->scheduledTransition_; }
 
     private:
@@ -102,9 +115,9 @@
         bool                                 bPauseParent_;
 
-        bool                                 bActive_;
-        bool                                 bSuspended_;
+        Operations                           operation_;
 
         GameState*                           parent_;
         GameState*                           activeChild_;
+        GameState*                           scheduledTransition_;
         std::map<std::string, GameState*>    allChildren_;
         std::map<GameState*, GameState*>     grandchildrenToChildren_;
Index: code/branches/gui/src/core/Script.cc
===================================================================
--- code/branches/gui/src/core/Script.cc	(revision 1665)
+++ code/branches/gui/src/core/Script.cc	(revision 1670)
@@ -198,5 +198,5 @@
       for (std::map<unsigned int, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
       {
-        if ((*it).second == expectedValue)
+        if (it->second == expectedValue)
           expectedValue = !expectedValue;
         else
Index: code/branches/gui/src/core/input/InputManager.cc
===================================================================
--- code/branches/gui/src/core/input/InputManager.cc	(revision 1665)
+++ code/branches/gui/src/core/input/InputManager.cc	(revision 1670)
@@ -348,5 +348,5 @@
         for (std::map<int, InputState*>::const_iterator it = inputStatesByPriority_.begin();
             it != inputStatesByPriority_.end(); ++it)
-            (*it).second->setNumOfJoySticks(joySticksSize_);
+            it->second->setNumOfJoySticks(joySticksSize_);
     }
 
@@ -506,7 +506,13 @@
     }
 
+    /**
+    @brief
+        Removes and destroys an InputState.
+    @return
+        True if state was removed immediately, false if postponed.
+    */
     void InputManager::_destroyState(InputState* state)
     {
-        assert(state);
+        assert(state && !(this->internalState_ & Ticking));
         std::map<int, InputState*>::iterator it = this->activeStates_.find(state->getPriority());
         if (it != this->activeStates_.end())
@@ -633,27 +639,35 @@
         internalState_ |= Ticking;
 
-        // check for states to leave (don't use unsigned int!)
-        for (int i = stateLeaveRequests_.size() - 1; i >= 0; --i)
-        {
-            stateLeaveRequests_[i]->onLeave();
+        // check for states to leave
+        for (std::set<InputState*>::reverse_iterator it = stateLeaveRequests_.rbegin();
+            it != stateLeaveRequests_.rend(); ++it)
+        {
+            (*it)->onLeave();
             // just to be sure that the state actually is registered
-            assert(inputStatesByName_.find(stateLeaveRequests_[i]->getName()) != inputStatesByName_.end());
-            
-            activeStates_.erase(stateLeaveRequests_[i]->getPriority());
+            assert(inputStatesByName_.find((*it)->getName()) != inputStatesByName_.end());
+
+            activeStates_.erase((*it)->getPriority());
             _updateActiveStates();
-            stateLeaveRequests_.pop_back();
-        }
-
-
-        // check for states to enter (don't use unsigned int!)
-        for (int i = stateEnterRequests_.size() - 1; i >= 0; --i)
+        }
+        stateLeaveRequests_.clear();
+
+        // check for states to enter
+        for (std::set<InputState*>::reverse_iterator it = stateEnterRequests_.rbegin();
+            it != stateEnterRequests_.rend(); ++it)
         {
             // just to be sure that the state actually is registered
-            assert(inputStatesByName_.find(stateEnterRequests_[i]->getName()) != inputStatesByName_.end());
-            
-            activeStates_[stateEnterRequests_[i]->getPriority()] = stateEnterRequests_[i];
+            assert(inputStatesByName_.find((*it)->getName()) != inputStatesByName_.end());
+
+            activeStates_[(*it)->getPriority()] = (*it);
             _updateActiveStates();
-            stateEnterRequests_[i]->onEnter();
-            stateEnterRequests_.pop_back();
+            (*it)->onEnter();
+        }
+        stateEnterRequests_.clear();
+
+        // check for states to destroy
+        for (std::set<InputState*>::reverse_iterator it = stateDestroyRequests_.rbegin();
+            it != stateDestroyRequests_.rend(); ++it)
+        {
+            _destroyState((*it));
         }
 
@@ -703,6 +717,6 @@
         for (std::map<int, InputState*>::const_iterator it = activeStates_.begin(); it != activeStates_.end(); ++it)
             for (unsigned int i = 0; i < devicesNum_; ++i)
-                if ((*it).second->isInputDeviceEnabled(i))
-                    activeStatesTop_[i] = (*it).second;
+                if (it->second->isInputDeviceEnabled(i))
+                    activeStatesTop_[i] = it->second;
 
         // update tickables (every state will only appear once)
@@ -1135,5 +1149,5 @@
     /**
     @brief
-        Removes an input state internally.
+        Removes and destroys an input state internally.
     @param name
         Name of the handler.
@@ -1142,6 +1156,7 @@
     @remarks
         You can't remove the internal states "empty", "calibrator" and "detector".
-    */
-    bool InputManager::destroyState(const std::string& name)
+        The removal process is being postponed if InputManager::tick() is currently running.
+    */
+    bool InputManager::requestDestroyState(const std::string& name)
     {
         if (name == "empty" || name == "calibrator" || name == "detector")
@@ -1153,5 +1168,18 @@
         if (it != inputStatesByName_.end())
         {
-            _destroyState((*it).second);
+            if (activeStates_.find(it->second->getPriority()) != activeStates_.end())
+            {
+                // The state is still active. We have to postpone
+                stateLeaveRequests_.insert(it->second);
+                stateDestroyRequests_.insert(it->second);
+            }
+            else if (this->internalState_ & Ticking)
+            {
+                // cannot remove state while ticking
+                stateDestroyRequests_.insert(it->second);
+            }
+            else
+                _destroyState(it->second);
+
             return true;
         }
@@ -1171,5 +1199,5 @@
         std::map<std::string, InputState*>::iterator it = inputStatesByName_.find(name);
         if (it != inputStatesByName_.end())
-            return (*it).second;
+            return it->second;
         else
             return 0;
@@ -1202,6 +1230,16 @@
         if (it != inputStatesByName_.end())
         {
-            stateEnterRequests_.push_back((*it).second);
-            return true;
+            // exists
+            if (activeStates_.find(it->second->getPriority()) == activeStates_.end())
+            {
+                // not active
+                if (stateDestroyRequests_.find(it->second) == stateDestroyRequests_.end())
+                {
+                    // not scheduled for destruction
+                    // set prevents a state being added multiple times
+                    stateEnterRequests_.insert(it->second);
+                    return true;
+                }
+            }
         }
         return false;
@@ -1222,6 +1260,11 @@
         if (it != inputStatesByName_.end())
         {
-            stateLeaveRequests_.push_back((*it).second);
-            return true;
+            // exists
+            if (activeStates_.find(it->second->getPriority()) != activeStates_.end())
+            {
+                // active
+                stateLeaveRequests_.insert(it->second);
+                return true;
+            }
         }
         return false;
Index: code/branches/gui/src/core/input/InputManager.h
===================================================================
--- code/branches/gui/src/core/input/InputManager.h	(revision 1665)
+++ code/branches/gui/src/core/input/InputManager.h	(revision 1670)
@@ -127,9 +127,11 @@
         }
 
-        bool destroyState          (const std::string& name);
         InputState* getState       (const std::string& name);
         InputState* getCurrentState();
+        bool requestDestroyState   (const std::string& name);
         bool requestEnterState     (const std::string& name);
         bool requestLeaveState     (const std::string& name);
+
+        void tick(float dt);
 
         static InputManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; }
@@ -167,6 +169,4 @@
         void _updateActiveStates();
         bool _configureInputState(InputState* state, const std::string& name, int priority);
-
-        void tick(float dt);
 
         // input events
@@ -204,6 +204,7 @@
         std::map<int, InputState*>          inputStatesByPriority_;
 
-        std::vector<InputState*>            stateEnterRequests_;   //!< Request to enter a new state
-        std::vector<InputState*>            stateLeaveRequests_;   //!< Request to leave the current state
+        std::set<InputState*>               stateEnterRequests_;   //!< Request to enter a new state
+        std::set<InputState*>               stateLeaveRequests_;   //!< Request to leave a running state
+        std::set<InputState*>               stateDestroyRequests_; //!< Request to destroy a state
 
         std::map<int, InputState*>          activeStates_;
