Index: code/trunk/cmake/LibraryConfig.cmake
===================================================================
--- code/trunk/cmake/LibraryConfig.cmake	(revision 3303)
+++ code/trunk/cmake/LibraryConfig.cmake	(revision 3304)
@@ -153,16 +153,8 @@
 # Expand the next statement if newer boost versions than 1.36.1 are released
 SET(Boost_ADDITIONAL_VERSIONS 1.37 1.37.0 1.38 1.38.0 1.39 1.39.0)
-# MSVC seems to be the only compiler requiring date_time
-IF(MSVC)
-  FIND_PACKAGE(Boost 1.34 REQUIRED thread filesystem date_time)
-ELSE(MSVC)
-  FIND_PACKAGE(Boost 1.34 REQUIRED thread filesystem)
-ENDIF(MSVC)
-# Boost 1.35 and newer also need the 'System' library
-IF(NOT Boost_VERSION LESS 103500)
-  FIND_PACKAGE(Boost 1.35 REQUIRED system)
-ENDIF()
+FIND_PACKAGE(Boost 1.35 REQUIRED thread filesystem system date_time)
 # No auto linking, so this option is useless anyway
 MARK_AS_ADVANCED(Boost_LIB_DIAGNOSTIC_DEFINITIONS)
+
 
 ####### Static/Dynamic linking options ##########
Index: code/trunk/src/core/CMakeLists.txt
===================================================================
--- code/trunk/src/core/CMakeLists.txt	(revision 3303)
+++ code/trunk/src/core/CMakeLists.txt	(revision 3304)
@@ -61,4 +61,8 @@
   TclBind.cc
   TclThreadManager.cc
+  
+  # multithreading
+  Thread.cc
+  ThreadPool.cc
 )
 ADD_SUBDIRECTORY(input)
Index: code/trunk/src/core/Clock.cc
===================================================================
--- code/trunk/src/core/Clock.cc	(revision 3303)
+++ code/trunk/src/core/Clock.cc	(revision 3304)
@@ -59,5 +59,5 @@
         tickDtFloat_ = static_cast<float>(tickDt_) / 1000000.0f;
 
-        if (timersTime > 0x7FFFFFF0)
+        if (timersTime > 0xFFFFFFFF/4)
         {
             // Ogre timer will overflow at 2^32 microseconds if unsigned long is 32 bit
@@ -74,5 +74,5 @@
     unsigned long long Clock::getRealMicroseconds() const
     {
-        return this->timer_->getMicroseconds();
+        return this->timer_->getMicroseconds() + this->storedTime_;
     }
 }
Index: code/trunk/src/core/ConsoleCommand.h
===================================================================
--- code/trunk/src/core/ConsoleCommand.h	(revision 3303)
+++ code/trunk/src/core/ConsoleCommand.h	(revision 3304)
@@ -32,4 +32,6 @@
 #include "CorePrereqs.h"
 
+#include <boost/preprocessor/cat.hpp>
+
 #include "ArgumentCompletionFunctions.h"
 #include "CommandExecutor.h"
@@ -39,27 +41,25 @@
 
 #define SetConsoleCommand(classname, function, bCreateShortcut) \
-    SetConsoleCommandAliasMulti(classname, function, #function, 0, bCreateShortcut)
+    SetConsoleCommandGeneric(classname, function, #function, bCreateShortcut)
 #define SetConsoleCommandAlias(classname, function, name, bCreateShortcut) \
-    SetConsoleCommandAliasMulti(classname, function, name, 0, bCreateShortcut)
-#define SetConsoleCommandAliasMulti(classname, function, name, number, bCreateShortcut) \
-    SetConsoleCommandGeneric(classname##function##consolecommand__##number, classname, orxonox::createConsoleCommand(orxonox::createFunctor(&classname::function), name), bCreateShortcut)
+    SetConsoleCommandGeneric(classname, function, name, bCreateShortcut)
 
-#define SetConsoleCommandGeneric(fakevariable, classname, command, bCreateShortcut) \
-    orxonox::ConsoleCommand& fakevariable = orxonox::ClassIdentifier<classname>::getIdentifier(#classname)->addConsoleCommand(command, bCreateShortcut)
+#define SetConsoleCommandGeneric(classname, function, name, bCreateShortcut) \
+    orxonox::ConsoleCommand& BOOST_PP_CAT(classname##function##consolecommand__, __LINE__) = orxonox::ClassIdentifier<classname>::getIdentifier(#classname)->addConsoleCommand(orxonox::createConsoleCommand(orxonox::createFunctor(&classname::function), name), bCreateShortcut)
 
 
 #define SetConsoleCommandShortcut(classname, function) \
-    SetConsoleCommandShortcutAliasMulti(classname, function, #function, 0)
+    SetConsoleCommandShortcutAliasGeneric(classname, function, #function)
 #define SetConsoleCommandShortcutAlias(classname, function, name) \
-    SetConsoleCommandShortcutAliasMulti(classname, function, name, 0)
-#define SetConsoleCommandShortcutAliasMulti(classname, function, name, number) \
-    SetConsoleCommandShortcutGeneric(function##consolecommand__##number, orxonox::createConsoleCommand(orxonox::createFunctor(&classname::function), name))
+    SetConsoleCommandShortcutAliasGeneric(classname, function, name)
+#define SetConsoleCommandShortcutAliasGeneric(classname, function, name) \
+    SetConsoleCommandShortcutGeneric(BOOST_PP_CAT(function##consolecommand__, __LINE__), orxonox::createConsoleCommand(orxonox::createFunctor(&classname::function), name))
 
 #define SetConsoleCommandShortcutExtern(function) \
-    SetConsoleCommandShortcutExternAliasMulti(function, #function, 0)
+    SetConsoleCommandShortcutExternAliasGeneric(function, #function)
 #define SetConsoleCommandShortcutExternAlias(function, name) \
-    SetConsoleCommandShortcutExternAliasMulti(function, name, 0)
-#define SetConsoleCommandShortcutExternAliasMulti(function, name, number) \
-    SetConsoleCommandShortcutGeneric(function##consolecommand__##number, orxonox::createConsoleCommand(orxonox::createFunctor(&function), name))
+    SetConsoleCommandShortcutExternAliasGeneric(function, name)
+#define SetConsoleCommandShortcutExternAliasGeneric(function, name) \
+    SetConsoleCommandShortcutGeneric(BOOST_PP_CAT(function##consolecommand__, __LINE__), orxonox::createConsoleCommand(orxonox::createFunctor(&function), name))
 
 #define SetConsoleCommandShortcutGeneric(fakevariable, command) \
Index: code/trunk/src/core/CorePrecompiledHeaders.h
===================================================================
--- code/trunk/src/core/CorePrecompiledHeaders.h	(revision 3303)
+++ code/trunk/src/core/CorePrecompiledHeaders.h	(revision 3304)
@@ -60,6 +60,4 @@
 #include <boost/shared_ptr.hpp> // 12
 #include <boost/preprocessor/cat.hpp> // 12
-// Included by both filesystem and thread but still relatively small
-#include <boost/iterator/iterator_facade.hpp> // 10
 
 // Just in case some header included windows.h
Index: code/trunk/src/core/CorePrereqs.h
===================================================================
--- code/trunk/src/core/CorePrereqs.h	(revision 3303)
+++ code/trunk/src/core/CorePrereqs.h	(revision 3304)
@@ -150,5 +150,4 @@
     class SubclassIdentifier;
     class TclBind;
-    struct TclInterpreterBundle;
     class TclThreadManager;
     class Template;
@@ -187,4 +186,8 @@
     class SimpleCommand;
     class SimpleInputState;
+
+    // multithreading
+    class Thread;
+    class ThreadPool;
 }
 
@@ -197,10 +200,15 @@
 
 // Boost
-namespace boost { namespace filesystem
-{
-    struct path_traits;
-    template <class String, class Traits> class basic_path;
-    typedef basic_path<std::string, path_traits> path;
-} }
+namespace boost
+{ 
+    namespace filesystem
+    {
+        struct path_traits;
+        template <class String, class Traits> class basic_path;
+        typedef basic_path<std::string, path_traits> path;
+    }
+    class thread;
+    class mutex;
+}
 
 // TinyXML and TinyXML++
Index: code/trunk/src/core/Executor.h
===================================================================
--- code/trunk/src/core/Executor.h	(revision 3303)
+++ code/trunk/src/core/Executor.h	(revision 3304)
@@ -246,4 +246,6 @@
             virtual ~ExecutorMember() {}
 
+            using Executor::operator();
+
             inline void operator()(T* object) const
                 { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
@@ -278,4 +280,6 @@
                 { ((FunctorMember<T>*)this->functor_)->setObject(object); }
 
+            using Executor::parse;
+
             bool parse(T* object, const std::string& params, const std::string& delimiter = " ") const
             {
Index: code/trunk/src/core/Game.cc
===================================================================
--- code/trunk/src/core/Game.cc	(revision 3303)
+++ code/trunk/src/core/Game.cc	(revision 3304)
@@ -40,4 +40,5 @@
 #include "util/Debug.h"
 #include "util/Exception.h"
+#include "util/Sleep.h"
 #include "util/SubString.h"
 #include "Clock.h"
@@ -95,8 +96,11 @@
             SetConfigValue(statisticsAvgLength_, 1000000)
                 .description("Sets the time in microseconds interval at which average fps, etc. gets calculated.");
+            SetConfigValue(fpsLimit_, 50)
+                .description("Sets the desired framerate (0 for no limit).");
         }
 
         unsigned int statisticsRefreshCycle_;
         unsigned int statisticsAvgLength_;
+        unsigned int fpsLimit_;
     };
 
@@ -192,9 +196,20 @@
 
         // START GAME
-        this->gameClock_->capture(); // first delta time should be about 0 seconds
+        // first delta time should be about 0 seconds
+        this->gameClock_->capture();
+        // A first item is required for the fps limiter
+        StatisticsTickInfo tickInfo = {0, 0};
+        statisticsTickTimes_.push_back(tickInfo);
         while (!this->bAbort_ && (!this->activeStates_.empty() || this->requestedStateNodes_.size() > 0))
         {
+            uint64_t currentTime = this->gameClock_->getRealMicroseconds();
+
+            uint64_t nextTickTime = statisticsTickTimes_.back().tickTime + static_cast<uint64_t>(1000000.0f / configuration_->fpsLimit_);
+            if (currentTime < nextTickTime)
+            {
+                usleep(nextTickTime - currentTime);
+                continue;
+            }
             this->gameClock_->capture();
-            uint64_t currentTime = this->gameClock_->getMicroseconds();
 
             // STATISTICS
Index: code/trunk/src/core/IRC.cc
===================================================================
--- code/trunk/src/core/IRC.cc	(revision 3303)
+++ code/trunk/src/core/IRC.cc	(revision 3304)
@@ -47,5 +47,5 @@
     {
         RegisterRootObject(IRC);
-        this->bundle_ = 0;
+        this->interpreter_ = 0;
     }
 
@@ -54,12 +54,12 @@
         unsigned int threadID = IRC_TCL_THREADID;
         TclThreadManager::createID(threadID);
-        this->bundle_ = TclThreadManager::getInstance().getInterpreterBundle(threadID);
+        this->interpreter_ = TclThreadManager::getInstance().getTclInterpreter(threadID);
 
         try
         {
-            this->bundle_->interpreter_->def("orxonox::irc::say", IRC::tcl_say, Tcl::variadic());
-            this->bundle_->interpreter_->def("orxonox::irc::privmsg", IRC::tcl_privmsg, Tcl::variadic());
-            this->bundle_->interpreter_->def("orxonox::irc::action", IRC::tcl_action, Tcl::variadic());
-            this->bundle_->interpreter_->def("orxonox::irc::info", IRC::tcl_info, Tcl::variadic());
+            this->interpreter_->def("orxonox::irc::say", IRC::tcl_say, Tcl::variadic());
+            this->interpreter_->def("orxonox::irc::privmsg", IRC::tcl_privmsg, Tcl::variadic());
+            this->interpreter_->def("orxonox::irc::action", IRC::tcl_action, Tcl::variadic());
+            this->interpreter_->def("orxonox::irc::info", IRC::tcl_info, Tcl::variadic());
         }
         catch (Tcl::tcl_error const &e)
@@ -81,5 +81,5 @@
     bool IRC::eval(const std::string& command)
     {
-        if (!IRC::getInstance().bundle_)
+        if (!IRC::getInstance().interpreter_)
         {
             IRC::getInstance().initialize();
@@ -90,5 +90,5 @@
         try
         {
-            IRC::getInstance().bundle_->interpreter_->eval(command);
+            IRC::getInstance().interpreter_->eval(command);
             return true;
         }
Index: code/trunk/src/core/IRC.h
===================================================================
--- code/trunk/src/core/IRC.h	(revision 3303)
+++ code/trunk/src/core/IRC.h	(revision 3304)
@@ -59,5 +59,5 @@
             ~IRC() {}
 
-            TclInterpreterBundle* bundle_;
+            Tcl::interpreter* interpreter_;
             std::string nickname_;
     };
Index: code/trunk/src/core/TclThreadManager.cc
===================================================================
--- code/trunk/src/core/TclThreadManager.cc	(revision 3303)
+++ code/trunk/src/core/TclThreadManager.cc	(revision 3304)
@@ -30,4 +30,7 @@
 
 #include <boost/bind.hpp>
+#include <boost/thread/condition.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
 #include <OgreTimer.h>
 #include <cpptcl/cpptcl.h>
@@ -56,7 +59,36 @@
     SetConsoleCommand(TclThreadManager, flush,   false).argumentCompleter(0, autocompletion::tclthreads());
 
+    struct TclInterpreterBundle
+    {
+        unsigned int id_;
+
+        std::list<std::string> queue_;
+        boost::mutex queueMutex_;
+
+        Tcl::interpreter* interpreter_;
+        std::string interpreterName_;
+        boost::try_mutex interpreterMutex_;
+
+        std::list<unsigned int> queriers_;
+        boost::mutex queriersMutex_;
+
+        bool running_;
+        boost::mutex runningMutex_;
+
+        bool finished_;
+        boost::mutex finishedMutex_;
+        boost::condition finishedCondition_;
+    };
+
+
+    static boost::thread::id threadID_g;
+    static boost::mutex bundlesMutex_g;
+    static boost::condition fullQueueCondition_g;
+    static boost::condition orxonoxEvalCondition_g;
+
     TclThreadManager* TclThreadManager::singletonRef_s = 0;
 
     TclThreadManager::TclThreadManager(Tcl::interpreter* interpreter)
+        : orxonoxInterpreterBundle_(new TclInterpreterBundle())
     {
         RegisterRootObject(TclThreadManager);
@@ -66,11 +98,7 @@
 
         this->threadCounter_ = 0;
-        this->orxonoxInterpreterBundle_.id_ = 0;
-        this->orxonoxInterpreterBundle_.interpreter_ = interpreter;
-#if (BOOST_VERSION >= 103500)
-        this->threadID_ = boost::this_thread::get_id();
-#else
-        //
-#endif
+        this->orxonoxInterpreterBundle_->id_ = 0;
+        this->orxonoxInterpreterBundle_->interpreter_ = interpreter;
+        threadID_g = boost::this_thread::get_id();
     }
 
@@ -79,5 +107,5 @@
         unsigned int threadID;
         {
-            boost::mutex::scoped_lock bundles_lock(this->bundlesMutex_);
+            boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
             if (this->interpreterBundles_.begin() == this->interpreterBundles_.end())
                 return;
@@ -87,4 +115,6 @@
         this->destroy(threadID);
 
+        delete this->orxonoxInterpreterBundle_;
+
         singletonRef_s = 0;
     }
@@ -92,5 +122,5 @@
     unsigned int TclThreadManager::create()
     {
-        boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_);
+        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
         TclThreadManager::getInstance().threadCounter_++;
         std::string name = multi_cast<std::string>(TclThreadManager::getInstance().threadCounter_);
@@ -132,20 +162,12 @@
                     if (bundle->finished_)
                     {
-                        boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_);
-#if (BOOST_VERSION >= 103500)
+                        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
                         boost::mutex::scoped_try_lock interpreter_lock(bundle->interpreterMutex_);
-#else
-                        boost::try_mutex::scoped_try_lock interpreter_lock(bundle->interpreterMutex_);
-#endif
                         try
                         {
                             while (!interpreter_lock.try_lock())
                             {
-                                TclThreadManager::getInstance().orxonoxEvalCondition_.notify_one();
-#if (BOOST_VERSION >= 103500)
+                                orxonoxEvalCondition_g.notify_one();
                                 boost::this_thread::yield();
-#else
-                                boost::thread::yield();
-#endif
                             }
                         } catch (...) {}
@@ -157,10 +179,6 @@
                 }
 
-                TclThreadManager::getInstance().orxonoxEvalCondition_.notify_one();
-#if (BOOST_VERSION >= 103500)
+                orxonoxEvalCondition_g.notify_one();
                 boost::this_thread::yield();
-#else
-                boost::thread::yield();
-#endif
             }
 
@@ -181,5 +199,5 @@
     std::string TclThreadManager::query(unsigned int threadID, const std::string& command)
     {
-        return TclThreadManager::getInstance().evalQuery(TclThreadManager::getInstance().orxonoxInterpreterBundle_.id_, threadID, command);
+        return TclThreadManager::getInstance().evalQuery(TclThreadManager::getInstance().orxonoxInterpreterBundle_->id_, threadID, command);
     }
 
@@ -191,6 +209,6 @@
         output += "\t\t";
         {
-            boost::mutex::scoped_lock queue_lock(TclThreadManager::getInstance().orxonoxInterpreterBundle_.queueMutex_);
-            output += multi_cast<std::string>(TclThreadManager::getInstance().orxonoxInterpreterBundle_.queue_.size());
+            boost::mutex::scoped_lock queue_lock(TclThreadManager::getInstance().orxonoxInterpreterBundle_->queueMutex_);
+            output += multi_cast<std::string>(TclThreadManager::getInstance().orxonoxInterpreterBundle_->queue_.size());
         }
         output += "\t\t";
@@ -198,5 +216,5 @@
         COUT(0) << output << std::endl;
 
-        boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_);
+        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
         for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = TclThreadManager::getInstance().interpreterBundles_.begin(); it != TclThreadManager::getInstance().interpreterBundles_.end(); ++it)
         {
@@ -209,9 +227,5 @@
             output += "\t\t";
             {
-#if (BOOST_VERSION >= 103500)
                 boost::mutex::scoped_try_lock interpreter_lock((*it).second->interpreterMutex_);
-#else
-                boost::try_mutex::scoped_try_lock interpreter_lock((*it).second->interpreterMutex_);
-#endif
                 if (interpreter_lock.try_lock())
                     output += "ready";
@@ -228,5 +242,5 @@
         if (threadID == 0)
         {
-            bundle = &TclThreadManager::getInstance().orxonoxInterpreterBundle_;
+            bundle = TclThreadManager::getInstance().orxonoxInterpreterBundle_;
             COUT(0) << "Queue dump of Orxonox:" << std::endl;
         }
@@ -254,5 +268,5 @@
         TclInterpreterBundle* bundle = 0;
         if (threadID == 0)
-            bundle = &TclThreadManager::getInstance().orxonoxInterpreterBundle_;
+            bundle = TclThreadManager::getInstance().orxonoxInterpreterBundle_;
         else
             if (!(bundle = TclThreadManager::getInstance().getInterpreterBundle(threadID)))
@@ -334,5 +348,5 @@
     TclInterpreterBundle* TclThreadManager::getInterpreterBundle(unsigned int threadID)
     {
-        boost::mutex::scoped_lock bundles_lock(this->bundlesMutex_);
+        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
         std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.find(threadID);
         if (it != this->interpreterBundles_.end())
@@ -347,4 +361,9 @@
     }
 
+    Tcl::interpreter* TclThreadManager::getTclInterpreter(unsigned int threadID)
+    {
+        return this->getInterpreterBundle(threadID)->interpreter_;
+    }
+
     std::string TclThreadManager::dumpList(const std::list<unsigned int>& list)
     {
@@ -362,18 +381,10 @@
     void TclThreadManager::error(const std::string& error)
     {
-#if (BOOST_VERSION >= 103500)
-        if (boost::this_thread::get_id() != this->threadID_)
-#else
-        if (boost::thread() != this->threadID_)
-#endif
-        {
-            boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
-            if (this->orxonoxInterpreterBundle_.queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
-            {
-#if (BOOST_VERSION >= 103500)
+        if (boost::this_thread::get_id() != threadID_g)
+        {
+            boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
+            if (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
+            {
                 boost::this_thread::yield();
-#else
-                boost::thread::yield();
-#endif
                 return;
             }
@@ -385,18 +396,10 @@
     void TclThreadManager::debug(const std::string& error)
     {
-#if (BOOST_VERSION >= 103500)
-        if (boost::this_thread::get_id() != this->threadID_)
-#else
-        if (boost::thread() != this->threadID_)
-#endif
-        {
-            boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
-            if (this->orxonoxInterpreterBundle_.queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
-            {
-#if (BOOST_VERSION >= 103500)
+        if (boost::this_thread::get_id() != threadID_g)
+        {
+            boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
+            if (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
+            {
                 boost::this_thread::yield();
-#else
-                boost::thread::yield();
-#endif
                 return;
             }
@@ -408,23 +411,23 @@
     void TclThreadManager::pushCommandToQueue(const std::string& command)
     {
-        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
-        while (this->orxonoxInterpreterBundle_.queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
-            this->fullQueueCondition_.wait(queue_lock);
-
-        this->orxonoxInterpreterBundle_.queue_.push_back(command);
+        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
+        while (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
+            fullQueueCondition_g.wait(queue_lock);
+
+        this->orxonoxInterpreterBundle_->queue_.push_back(command);
     }
 
     void TclThreadManager::forceCommandToFrontOfQueue(const std::string& command)
     {
-        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
-        this->orxonoxInterpreterBundle_.queue_.push_front(command);
+        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
+        this->orxonoxInterpreterBundle_->queue_.push_front(command);
     }
 
     std::string TclThreadManager::popCommandFromQueue()
     {
-        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
-        std::string temp = this->orxonoxInterpreterBundle_.queue_.front();
-        this->orxonoxInterpreterBundle_.queue_.pop_front();
-        this->fullQueueCondition_.notify_one();
+        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
+        std::string temp = this->orxonoxInterpreterBundle_->queue_.front();
+        this->orxonoxInterpreterBundle_->queue_.pop_front();
+        fullQueueCondition_g.notify_one();
         return temp;
     }
@@ -432,6 +435,6 @@
     bool TclThreadManager::queueIsEmpty()
     {
-        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
-        return this->orxonoxInterpreterBundle_.queue_.empty();
+        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
+        return this->orxonoxInterpreterBundle_->queue_.empty();
     }
 
@@ -505,12 +508,8 @@
         if (querier)
         {
-            if (this->updateQueriersList(querier, &this->orxonoxInterpreterBundle_))
-            {
-#if (BOOST_VERSION >= 103500)
-                boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
-#else
-                boost::try_mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
-#endif
-                this->orxonoxEvalCondition_.wait(interpreter_lock);
+            if (this->updateQueriersList(querier, this->orxonoxInterpreterBundle_))
+            {
+                boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_->interpreterMutex_);
+                orxonoxEvalCondition_g.wait(interpreter_lock);
 
                 if (!CommandExecutor::execute(command, false))
@@ -521,6 +520,6 @@
             }
 
-            boost::mutex::scoped_lock queriers_lock(this->orxonoxInterpreterBundle_.queriersMutex_);
-            this->orxonoxInterpreterBundle_.queriers_.clear();
+            boost::mutex::scoped_lock queriers_lock(this->orxonoxInterpreterBundle_->queriersMutex_);
+            this->orxonoxInterpreterBundle_->queriers_.clear();
         }
         return output;
@@ -533,5 +532,5 @@
             target = this->getInterpreterBundle(threadID);
         else
-            target = &this->orxonoxInterpreterBundle_;
+            target = this->orxonoxInterpreterBundle_;
 
         std::string output = "";
@@ -542,5 +541,5 @@
                 querier = this->getInterpreterBundle(querierID);
             else
-                querier = &this->orxonoxInterpreterBundle_;
+                querier = this->orxonoxInterpreterBundle_;
 
             if (querier)
@@ -548,9 +547,5 @@
                 if (this->updateQueriersList(querier, target))
                 {
-#if (BOOST_VERSION >= 103500)
                     boost::mutex::scoped_try_lock interpreter_lock(target->interpreterMutex_);
-#else
-                    boost::try_mutex::scoped_try_lock interpreter_lock(target->interpreterMutex_);
-#endif
                     bool successfullyLocked = false;
                     try
@@ -562,9 +557,5 @@
                             while (!interpreter_lock.try_lock())
                             {
-#if (BOOST_VERSION >= 103500)
                                 boost::this_thread::yield();
-#else
-                                boost::thread::yield();
-#endif
                             }
 
@@ -599,14 +590,10 @@
     {
         {
-            this->orxonoxEvalCondition_.notify_one();
-#if (BOOST_VERSION >= 103500)
+            orxonoxEvalCondition_g.notify_one();
             boost::this_thread::yield();
-#else
-            boost::thread::yield();
-#endif
-        }
-
-        {
-            boost::mutex::scoped_lock bundles_lock(this->bundlesMutex_);
+        }
+
+        {
+            boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
             for (std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it)
             {
@@ -626,9 +613,5 @@
 
         {
-#if (BOOST_VERSION >= 103500)
-            boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
-#else
-            boost::try_mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
-#endif
+            boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_->interpreterMutex_);
             unsigned long maxtime = static_cast<unsigned long>(time.getDeltaTime() * 1000000 * TCLTHREADMANAGER_MAX_CPU_USAGE);
             Ogre::Timer timer;
@@ -644,5 +627,5 @@
     std::list<unsigned int> TclThreadManager::getThreadList() const
     {
-        boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_);
+        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
         std::list<unsigned int> threads;
         for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it)
@@ -654,9 +637,5 @@
     {
         TclThreadManager::getInstance().debug("TclThread_execute: " + command);
-#if (BOOST_VERSION >= 103500)
         boost::mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_);
-#else
-        boost::try_mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_);
-#endif
         try
         {
Index: code/trunk/src/core/TclThreadManager.h
===================================================================
--- code/trunk/src/core/TclThreadManager.h	(revision 3303)
+++ code/trunk/src/core/TclThreadManager.h	(revision 3304)
@@ -32,36 +32,14 @@
 #include "CorePrereqs.h"
 
+#include <cassert>
 #include <list>
 #include <map>
 #include <string>
-#include <boost/thread/condition.hpp>
-#include <boost/thread/mutex.hpp>
-#include <boost/thread/thread.hpp>
-
 #include "core/OrxonoxClass.h"
 
 namespace orxonox
 {
-    struct _CoreExport TclInterpreterBundle
-    {
-        unsigned int id_;
-
-        std::list<std::string> queue_;
-        boost::mutex queueMutex_;
-
-        Tcl::interpreter* interpreter_;
-        std::string interpreterName_;
-        boost::try_mutex interpreterMutex_;
-
-        std::list<unsigned int> queriers_;
-        boost::mutex queriersMutex_;
-
-        bool running_;
-        boost::mutex runningMutex_;
-
-        bool finished_;
-        boost::mutex finishedMutex_;
-        boost::condition finishedCondition_;
-    };
+    // Internal struct
+    struct TclInterpreterBundle;
 
     class _CoreExport TclThreadManager : public OrxonoxClass
@@ -101,4 +79,5 @@
 
             Tcl::interpreter* createNewTclInterpreter(const std::string& threadID);
+            Tcl::interpreter* getTclInterpreter(unsigned int threadID);
             TclInterpreterBundle* getInterpreterBundle(unsigned int threadID);
             std::string dumpList(const std::list<unsigned int>& list);
@@ -119,14 +98,6 @@
 
             unsigned int threadCounter_;
-            TclInterpreterBundle orxonoxInterpreterBundle_;
+            TclInterpreterBundle* orxonoxInterpreterBundle_;
             std::map<unsigned int, TclInterpreterBundle*> interpreterBundles_;
-            boost::mutex bundlesMutex_;
-            boost::condition fullQueueCondition_;
-            boost::condition orxonoxEvalCondition_;
-#if (BOOST_VERSION >= 103500)
-            boost::thread::id threadID_;
-#else
-            boost::thread threadID_;
-#endif
 
             static TclThreadManager* singletonRef_s;
Index: code/trunk/src/core/Thread.cc
===================================================================
--- code/trunk/src/core/Thread.cc	(revision 3304)
+++ code/trunk/src/core/Thread.cc	(revision 3304)
@@ -0,0 +1,129 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Oliver Scheuss
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "Thread.h"
+
+#include <cassert>
+#include <boost/thread/thread.hpp>
+#include <boost/bind.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread_time.hpp>
+
+#include "util/Sleep.h"
+#include "Executor.h"
+
+namespace orxonox
+{
+    boost::posix_time::millisec THREAD_WAIT_BEFORE_DETACH(1000);
+    
+    
+    Thread::Thread():
+        executor_(0),
+        isWorking_(false),
+        stopThread_(false)
+    {
+        this->executorMutex_ = new boost::mutex;
+        this->isWorkingMutex_ = new boost::mutex;
+        this->stopThreadMutex_ = new boost::mutex;
+        this->workerThread_ = new boost::thread( boost::bind(&Thread::threadLoop, this) );
+    }
+    
+    Thread::~Thread()
+    {
+        this->stopThreadMutex_->lock();
+        this->stopThread_ = true;
+        this->stopThreadMutex_->unlock();
+        if( !this->workerThread_->timed_join( THREAD_WAIT_BEFORE_DETACH ) )
+            assert(0); // this should not happen
+        delete this->workerThread_;
+        delete this->executorMutex_;
+        delete this->stopThreadMutex_;
+        delete this->isWorkingMutex_;
+    }
+    
+    bool Thread::isWorking()
+    {
+      this->isWorkingMutex_->lock();
+      bool isWorking = this->isWorking_;
+      this->isWorkingMutex_->unlock();
+      return isWorking;
+    }
+    
+    bool Thread::evaluateExecutor( Executor* executor )
+    {
+        this->isWorkingMutex_->lock();
+        this->isWorking_=true;
+        this->isWorkingMutex_->unlock();
+        this->executorMutex_->lock();
+        this->executor_ = executor;
+        this->executorMutex_->unlock();
+        return true;
+    }
+    
+    void Thread::threadLoop()
+    {
+        bool stopThread = false;
+        while( !stopThread )
+        {
+            //this->executorMutex_->lock();
+            Executor* executor = this->executor_;
+            //this->executorMutex_->unlock();
+            if( executor )
+            {
+                (*executor)();
+                this->executorMutex_->lock();
+                delete this->executor_;
+                this->executor_ = 0;
+                this->executorMutex_->unlock();
+                this->isWorkingMutex_->lock();
+                this->isWorking_=false;
+                this->isWorkingMutex_->unlock();
+            }
+            else
+            {
+                this->workerThread_->yield();
+            }
+            //this->stopThreadMutex_->lock();
+            stopThread = this->stopThread_;
+            //this->stopThreadMutex_->unlock();
+        }
+    }
+    
+    void Thread::waitUntilFinished()
+    {
+        bool stillWorking = true;
+        while( stillWorking )
+        {
+            this->isWorkingMutex_->lock();
+            stillWorking = this->isWorking_;
+            this->isWorkingMutex_->unlock();
+            if( stillWorking )
+                msleep( 1 );
+        }
+    }
+}
Index: code/trunk/src/core/Thread.h
===================================================================
--- code/trunk/src/core/Thread.h	(revision 3304)
+++ code/trunk/src/core/Thread.h	(revision 3304)
@@ -0,0 +1,64 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Oliver Scheuss
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _Thread_H__
+#define _Thread_H__
+
+#include "CorePrereqs.h"
+
+namespace boost{
+  class recursive_mutex;
+}
+
+ namespace orxonox
+{
+    class _CoreExport Thread
+    {
+    public:
+        Thread();
+        virtual ~Thread();
+
+        bool isWorking();
+        void waitUntilFinished();
+        bool evaluateExecutor( Executor* executor );
+
+    private:
+        void            threadLoop();
+        
+        Executor*       executor_;
+        bool            isWorking_;
+        bool            stopThread_;
+        boost::thread*  workerThread_;
+        boost::mutex*   executorMutex_;
+        boost::mutex*     isWorkingMutex_;
+        boost::mutex*   stopThreadMutex_;
+    };
+
+ }
+
+#endif /* _Thread_H__ */
Index: code/trunk/src/core/ThreadPool.cc
===================================================================
--- code/trunk/src/core/ThreadPool.cc	(revision 3304)
+++ code/trunk/src/core/ThreadPool.cc	(revision 3304)
@@ -0,0 +1,115 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Oliver Scheuss
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "ThreadPool.h"
+#include "Thread.h"
+#include <cassert>
+
+namespace orxonox
+{
+
+    ThreadPool::ThreadPool()
+    {
+    }
+    
+    ThreadPool::~ThreadPool()
+    {
+        unsigned int a = this->setNrOfThreads(0);
+        assert(a == 0);
+    }
+    
+    void ThreadPool::addThreads( unsigned int nr )
+    {
+        for( unsigned int i=0; i<nr; i++ )
+            this->threadPool_.push_back(new Thread());
+    }
+    unsigned int ThreadPool::removeThreads( unsigned int nr )
+    {
+        unsigned int i=0;
+        std::vector<Thread*>::iterator it;
+        for( it = this->threadPool_.begin(); it != threadPool_.end() && i<nr; )
+        {
+            if( ! (*it)->isWorking() )
+            {
+                Thread* temp = *it;
+                it=this->threadPool_.erase( it );
+                delete temp;
+                ++i;
+            }
+            else
+              ++it;
+        }
+        return i;
+    }
+    unsigned int ThreadPool::setNrOfThreads( unsigned int nr )
+    {
+        unsigned int currentsize = this->threadPool_.size();
+        if ( nr < currentsize )
+            return currentsize - removeThreads( currentsize - nr );
+        else if ( nr == currentsize )
+            return currentsize;
+        else
+        {
+            addThreads( nr - currentsize );
+            return nr;
+        }
+    }
+    
+    bool ThreadPool::passFunction( Executor* executor, bool addThread )
+    {
+        std::vector<Thread*>::iterator it;
+        for ( it=this->threadPool_.begin(); it!=this->threadPool_.end(); ++it )
+        {
+            if ( ! (*it)->isWorking() )
+            {
+                bool b = (*it)->evaluateExecutor( executor );
+                assert(b); // if b is false then there is some code error
+                return true;
+            }
+        }
+        if ( addThread )
+        {
+            addThreads( 1 );
+            bool b = this->threadPool_.back()->evaluateExecutor( executor ); // access the last element
+            assert(b);
+            return true;
+        }
+        else
+            return false;
+    }
+    
+    void ThreadPool::synchronise()
+    {
+        std::vector<Thread*>::iterator it;
+        for ( it=this->threadPool_.begin(); it!=this->threadPool_.end(); ++it )
+        {
+            (*it)->waitUntilFinished();
+        }
+    }
+
+}
Index: code/trunk/src/core/ThreadPool.h
===================================================================
--- code/trunk/src/core/ThreadPool.h	(revision 3304)
+++ code/trunk/src/core/ThreadPool.h	(revision 3304)
@@ -0,0 +1,57 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Oliver Scheuss
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _ThreadPool_H__
+#define _ThreadPool_H__
+
+#include "CorePrereqs.h"
+
+#include <vector>
+
+ namespace orxonox
+{
+    class _CoreExport ThreadPool
+    {
+    public:
+        ThreadPool();
+        virtual ~ThreadPool();
+        
+        void addThreads( unsigned int nr );
+        unsigned int removeThreads( unsigned int nr );
+        unsigned int setNrOfThreads( unsigned int nr );
+        
+        bool passFunction( Executor* executor, bool addThread=false );
+        void synchronise();
+        
+    private:
+        std::vector<Thread*> threadPool_;
+        
+    };
+}
+
+#endif /* _ThreadPool_H__ */
Index: code/trunk/src/network/CMakeLists.txt
===================================================================
--- code/trunk/src/network/CMakeLists.txt	(revision 3303)
+++ code/trunk/src/network/CMakeLists.txt	(revision 3304)
@@ -31,5 +31,4 @@
   NetworkFunction.cc
   Host.cc
-  PacketBuffer.cc
   Server.cc
   ServerConnection.cc
Index: code/trunk/src/network/ClientInformation.h
===================================================================
--- code/trunk/src/network/ClientInformation.h	(revision 3303)
+++ code/trunk/src/network/ClientInformation.h	(revision 3304)
@@ -87,4 +87,5 @@
     static ClientInformation *findClient(ENetAddress *address, bool look_backwards=false);
     static ClientInformation *getBegin(){return head_;}
+    static bool hasClients(){ return ClientInformation::head_!=0; }
 
     bool setSynched(bool s);
Index: code/trunk/src/network/Connection.cc
===================================================================
--- code/trunk/src/network/Connection.cc	(revision 3303)
+++ code/trunk/src/network/Connection.cc	(revision 3304)
@@ -31,5 +31,4 @@
 #include <cassert>
 #include <enet/enet.h>
-#include <OgreTimer.h>
 #include "packet/Packet.h"
 
@@ -77,7 +76,6 @@
     
     assert(this->host_);
-    Ogre::Timer timer;
 
-    while( timer.getMilliseconds()<NETWORK_MAX_QUEUE_PROCESS_TIME && enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
+    while( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
     {
       switch(event.type){
Index: code/trunk/src/network/GamestateClient.cc
===================================================================
--- code/trunk/src/network/GamestateClient.cc	(revision 3303)
+++ code/trunk/src/network/GamestateClient.cc	(revision 3304)
@@ -52,4 +52,9 @@
 
   GamestateClient::~GamestateClient() {
+      std::map<unsigned int, packet::Gamestate *>::iterator it;
+      for ( it = this->gamestateMap_.begin(); it != this->gamestateMap_.end(); ++it )
+          delete (*it).second;
+      if( this->tempGamestate_ )
+          delete this->tempGamestate_;
   }
 
Index: code/trunk/src/network/GamestateClient.h
===================================================================
--- code/trunk/src/network/GamestateClient.h	(revision 3303)
+++ code/trunk/src/network/GamestateClient.h	(revision 3304)
@@ -73,5 +73,4 @@
     std::map<unsigned int, packet::Gamestate *> gamestateMap_;
     packet::Gamestate *tempGamestate_; // we save the received gamestates here during processQueue
-    unsigned char *shipCache_;
 
   };
Index: code/trunk/src/network/GamestateManager.cc
===================================================================
--- code/trunk/src/network/GamestateManager.cc	(revision 3303)
+++ code/trunk/src/network/GamestateManager.cc	(revision 3304)
@@ -42,6 +42,10 @@
 
 #include <cassert>
+#include <queue>
+// #include <boost/thread/mutex.hpp>
 
 #include "util/Debug.h"
+#include "core/Executor.h"
+#include "core/ThreadPool.h"
 #include "ClientInformation.h"
 #include "packet/Acknowledgement.h"
@@ -56,4 +60,6 @@
   {
     trafficControl_ = new TrafficControl();
+//     threadMutex_ = new boost::mutex();
+//     threadPool_ = new ThreadPool();
   }
 
@@ -61,8 +67,17 @@
   {
     if( this->reference )
-      delete this->reference;
-    for( std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.begin(); it != gamestateQueue.end(); it++ )
+        delete this->reference;std::map<unsigned int, packet::Gamestate*>::iterator it;
+    for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it )
       delete (*it).second;
-    delete trafficControl_;
+    std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator it1;
+    std::map<unsigned int, packet::Gamestate*>::iterator it2;
+    for( it1 = gamestateMap_.begin(); it1 != gamestateMap_.end(); ++it1 )
+    {
+      for( it2 = it1->second.begin(); it2 != it1->second.end(); ++it2 )
+        delete (*it2).second;
+    }
+    delete this->trafficControl_;
+//     delete this->threadMutex_;
+//     delete this->threadPool_;
   }
 
@@ -84,4 +99,6 @@
 
   bool GamestateManager::processGamestates(){
+    if( this->gamestateQueue.empty() )
+        return true;
     std::map<unsigned int, packet::Gamestate*>::iterator it;
     // now push only the most recent gamestates we received (ignore obsolete ones)
@@ -109,30 +126,79 @@
     return true;
   }
-
-
-  packet::Gamestate *GamestateManager::popGameState(unsigned int clientID) {
+  
+  void GamestateManager::sendGamestates()
+  {
+    ClientInformation *temp = ClientInformation::getBegin();
+    std::queue<packet::Gamestate*> clientGamestates;
+    while(temp != NULL){
+      if( !(temp->getSynched()) ){
+        COUT(5) << "Server: not sending gamestate" << std::endl;
+        temp=temp->next();
+        if(!temp)
+          break;
+        continue;
+      }
+      COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
+      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
+      int cid = temp->getID(); //get client id
+      
+      unsigned int gID = temp->getGamestateID();
+      if(!reference)
+        return;
+      
+      packet::Gamestate *client=0;
+      if(gID != GAMESTATEID_INITIAL){
+        assert(gamestateMap_.find(cid)!=gamestateMap_.end());
+        std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[cid].find(gID);
+        if(it!=gamestateMap_[cid].end())
+        {
+          client = it->second;
+        }
+      }
+      
+      clientGamestates.push(0);
+      finishGamestate( cid, &clientGamestates.back(), client, reference );
+      //FunctorMember<GamestateManager>* functor = 
+//       ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate) );
+//       executor->setObject(this);
+//       executor->setDefaultValues( cid, &clientGamestates.back(), client, reference );
+//       (*static_cast<Executor*>(executor))();
+//       this->threadPool_->passFunction( executor, true );
+//       (*functor)( cid, &(clientGamestates.back()), client, reference );
+      
+      temp = temp->next();
+    }
+    
+//     threadPool_->synchronise();
+    
+    while( !clientGamestates.empty() )
+    {
+      if(clientGamestates.front())
+        clientGamestates.front()->send();
+      clientGamestates.pop();
+    }
+  }
+
+
+  void GamestateManager::finishGamestate( unsigned int clientID, packet::Gamestate** destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
     //why are we searching the same client's gamestate id as we searched in
     //Server::sendGameState?
-    packet::Gamestate *gs;
-    unsigned int gID = ClientInformation::findClient(clientID)->getGamestateID();
-    if(!reference)
-      return 0;
-    gs = reference->doSelection(clientID, 10000);
     // save the (undiffed) gamestate in the clients gamestate map
-    gamestateMap_[clientID][gs->getID()]=gs;
     //chose wheather the next gamestate is the first or not
-    packet::Gamestate *client=0;
-    if(gID != GAMESTATEID_INITIAL){
-      assert(gamestateMap_.find(clientID)!=gamestateMap_.end());
-      std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[clientID].find(gID);
-      if(it!=gamestateMap_[clientID].end())
-      {
-        client = it->second;
-      }
-    }
-    if(client){
+    
+    packet::Gamestate *gs = gamestate->doSelection(clientID, 20000);
+//     packet::Gamestate *gs = new packet::Gamestate(*gamestate);
+//     packet::Gamestate *gs = new packet::Gamestate();
+//     gs->collectData( id_, 0x1 );
+//     this->threadMutex_->lock();
+    gamestateMap_[clientID][gamestate->getID()]=gs;
+//     this->threadMutex_->unlock();
+    
+    if(base)
+    {
+        
 //       COUT(3) << "diffing" << std::endl;
 //       packet::Gamestate* gs1  = gs;
-      packet::Gamestate *diffed = gs->diff(client);
+      packet::Gamestate *diffed = gs->diff(base);
       //packet::Gamestate *gs2 = diffed->undiff(gs);
 //       assert(*gs == *gs2);
@@ -143,15 +209,17 @@
     }
     else{
-//       COUT(3) << "not diffing" << std::endl;
       gs = new packet::Gamestate(*gs);
     }
+    
+    
     bool b = gs->compressData();
     assert(b);
-    COUT(4) << "sending gamestate with id " << gs->getID();
-    if(gs->isDiffed())
-    COUT(4) << " and baseid " << gs->getBaseID() << endl;
-    else
-    COUT(4) << endl;
-    return gs;
+//     COUT(4) << "sending gamestate with id " << gs->getID();
+//     if(gamestate->isDiffed())
+//     COUT(4) << " and baseid " << gs->getBaseID() << endl;
+//     else
+//     COUT(4) << endl;
+    gs->setClientID(clientID);
+    *destgamestate = gs;
   }
 
@@ -175,5 +243,5 @@
 
     assert(curid==GAMESTATEID_INITIAL || curid<gamestateID);
-    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
+    COUT(5) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
     std::map<unsigned int, packet::Gamestate*>::iterator it;
     for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; ){
Index: code/trunk/src/network/GamestateManager.h
===================================================================
--- code/trunk/src/network/GamestateManager.h	(revision 3303)
+++ code/trunk/src/network/GamestateManager.h	(revision 3304)
@@ -45,4 +45,5 @@
 #include <map>
 #include "GamestateHandler.h"
+#include "core/CorePrereqs.h"
 
 namespace orxonox
@@ -73,5 +74,7 @@
     bool processGamestates();
     bool update();
-    packet::Gamestate *popGameState(unsigned int clientID);
+    void sendGamestates();
+//     packet::Gamestate *popGameState(unsigned int clientID);
+    void finishGamestate( unsigned int clientID, packet::Gamestate** destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate );
 
     bool getSnapshot();
@@ -79,14 +82,14 @@
     bool ack(unsigned int gamestateID, unsigned int clientID);
     void removeClient(ClientInformation *client);
-    private:
+  private:
     bool processGamestate(packet::Gamestate *gs);
 
     std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> > gamestateMap_;
-    //std::map<int, packet::Gamestate*> gamestateMap; //map gsID to gamestate*
-    //std::map<int, int> gamestateUsed; // save the number of clients, that use the specific gamestate
     std::map<unsigned int, packet::Gamestate*> gamestateQueue;
     packet::Gamestate *reference;
     TrafficControl *trafficControl_;
     unsigned int id_;
+//     boost::mutex* threadMutex_;
+    ThreadPool*   /*thread*/Pool_;
   };
 
Index: code/trunk/src/network/Host.cc
===================================================================
--- code/trunk/src/network/Host.cc	(revision 3303)
+++ code/trunk/src/network/Host.cc	(revision 3304)
@@ -74,19 +74,4 @@
 }
 
-
-// bool Host::chat(std::string& message){
-//   if(!instance_)
-//     return false;
-//   packet::Chat *c = new packet::Chat(message, getPlayerID());
-//   return instance_->sendChat(c);
-// }
-
-// bool Host::receiveChat(packet::Chat *message, unsigned int clientID){
-//   if(instance_)
-//     return instance_->processChat(message, clientID);
-//   else
-//     return false;
-// }
-
 /**
  * This function returns the ID of the player
Index: code/trunk/src/network/NetworkFunction.cc
===================================================================
--- code/trunk/src/network/NetworkFunction.cc	(revision 3303)
+++ code/trunk/src/network/NetworkFunction.cc	(revision 3304)
@@ -56,4 +56,11 @@
   
   
+  void NetworkFunctionBase::destroyAllNetworkFunctions()
+  {
+    std::map<std::string, NetworkFunctionBase*>::iterator it;
+    for( it=NetworkFunctionBase::nameMap_.begin(); it!=NetworkFunctionBase::nameMap_.end(); ++it )
+      delete it->second;
+  }
+  
   
   NetworkFunctionStatic::NetworkFunctionStatic(FunctorStatic* functor, const std::string& name, const NetworkFunctionPointer& p):
Index: code/trunk/src/network/NetworkFunction.h
===================================================================
--- code/trunk/src/network/NetworkFunction.h	(revision 3303)
+++ code/trunk/src/network/NetworkFunction.h	(revision 3304)
@@ -37,4 +37,5 @@
 #include <string>
 #include <boost/preprocessor/cat.hpp>
+#include <boost/static_assert.hpp>
 
 #include "core/OrxonoxClass.h"
@@ -46,7 +47,7 @@
 {
 
-#ifdef ORXONOX_COMPILER_GCC
+#if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32)
 static const unsigned int MAX_FUNCTION_POINTER_SIZE = 8;
-#else //ORXONOX_COMPILER_GCC
+#else
 static const unsigned int MAX_FUNCTION_POINTER_SIZE = 16;
 #endif //ORXONOX_COMPILER_GCC
@@ -57,5 +58,5 @@
   bool operator<(const NetworkFunctionPointer& b) const
   {
-#ifdef ORXONOX_COMPILER_GCC
+#if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32)
     return pointer[0]<b.pointer[0] ? true : pointer[1]<b.pointer[1];
 #else //ORXONOX_COMPILER_GCC
@@ -80,4 +81,6 @@
     
     static inline void setNetworkID(const std::string& name, uint32_t id){ assert( nameMap_.find(name)!=nameMap_.end() ); nameMap_[name]->setNetworkID(id); }
+    
+    static void destroyAllNetworkFunctions();
     
   protected:
@@ -193,5 +196,6 @@
 template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr)
 {
-  memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T));
+  if( sizeof(NetworkFunctionPointer)-sizeof(T) > 0)
+    memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T));
   T p2 = ptr;
   memcpy( &destptr, &p2, sizeof(T) );
@@ -202,4 +206,5 @@
 template<class T> inline void* registerStaticNetworkFunctionFct( T ptr, const std::string& name )
 {
+  BOOST_STATIC_ASSERT( sizeof(T)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for static functions than defined above
   NetworkFunctionPointer destptr;
   copyPtr( ptr, destptr );
@@ -210,4 +215,5 @@
 template<class T, class PT> inline void* registerMemberNetworkFunctionFct( PT ptr, const std::string& name )
 {
+  BOOST_STATIC_ASSERT( sizeof(PT)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for a specific kind of member functions than defined above
   NetworkFunctionPointer destptr;
   copyPtr( ptr, destptr );
Index: code/trunk/src/network/NetworkPrereqs.h
===================================================================
--- code/trunk/src/network/NetworkPrereqs.h	(revision 3303)
+++ code/trunk/src/network/NetworkPrereqs.h	(revision 3304)
@@ -102,5 +102,4 @@
   template <class T> class NetworkMemeberFunction;
   struct NetworkFunctionPointer;
-  class PacketBuffer;
   class Server;
   class ServerConnection;
Index: code/trunk/src/network/PacketBuffer.cc
===================================================================
--- code/trunk/src/network/PacketBuffer.cc	(revision 3303)
+++ 	(revision )
@@ -1,146 +1,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Oliver Scheuss, (C) 2007
- *   Co-authors:
- *      ...
- *
- */
-
-// C++ PacketBuffer
-// d
-// Author Oliver Scheuss
-
-#include "PacketBuffer.h"
-
-//#include <iostream>
-#include <boost/thread/recursive_mutex.hpp>
-
-namespace orxonox
-{
-  static boost::recursive_mutex packetBuffer_mutex_g;
-
-  PacketBuffer::PacketBuffer() {
-    closed=false;
-    first=NULL;
-    last=NULL;
-  }
-  //this is needed in order to make the packetbuffer threadsafe
-
-
-  bool PacketBuffer::push(ENetEvent *ev) {
-    boost::recursive_mutex::scoped_lock lock(packetBuffer_mutex_g);
-    //std::cout << "event size inside packetbuffer " << ev->packet->dataLength << std::endl;
-    //   if(closed)
-    //     return false;
-    // first element?
-    if(first==NULL){
-      first=new QueueItem;
-      last=first;
-      last->next=NULL;
-      // change this!!!!!!!  ---- we are not doing stl so we won't change this
-      last->event = ev;
-      //last->address = ev->peer->address;
-    } else {
-      //insert a new element at the bottom
-      last->next = new QueueItem;
-      last=last->next;
-      // initialize last->next
-      last->next=NULL;
-      // save the packet to the new element
-      last->event = ev;
-      //last->address = ev->peer->address;
-    }
-    lock.unlock();
-    return true;
-  }
-
-  //returns the first element in the list without deleting it but
-  //moving first pointer to next element
-  /*ENetPacket *PacketBuffer::pop() {
-    ENetAddress address;
-    return pop(address);
-  }*/
-  
-  ENetEvent *PacketBuffer::pop(){
-    boost::recursive_mutex::scoped_lock lock(packetBuffer_mutex_g);
-    //std::cout << "packetbuffer pop(address)" << std::endl;
-    if(first!=NULL /*&& !closed*/){
-      QueueItem *temp = first;
-      // get packet
-      ENetEvent *ev=first->event;
-      //address = first->address;
-      // remove first element
-      first = first->next;
-      delete temp;
-      lock.unlock();
-      //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl;
-      return ev;
-    } else{
-      lock.unlock();
-      return NULL;
-    }
-  }
-
-  /*ENetPacket *PacketBuffer::pop(ENetAddress &address) {
-    boost::recursive_mutex::scoped_lock lock(packetBuffer_mutex_g);
-    //std::cout << "packetbuffer pop(address)" << std::endl;
-    if(first!=NULL ){
-      QueueItem *temp = first;
-      // get packet
-      ENetPacket *pck=first->event->packet;
-      address = first->event->peer->address;
-      // remove first element
-      first = first->next;
-      delete temp;
-      lock.unlock();
-      //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl;
-      return pck;
-    } else{
-      lock.unlock();
-      return NULL;
-    }
-  }*/
-
-  bool PacketBuffer::isEmpty() {
-    return (first==NULL);
-  }
-
-  void PacketBuffer::print() {
-    QueueItem *temp=first;
-    while(temp!=NULL){
-      //    std::cout << temp->packet->data << std::endl;
-      temp=temp->next;
-    }
-
-  }
-
-  bool PacketBuffer::isClosed() {
-    return closed;
-  }
-
-  void PacketBuffer::setClosed(bool value){
-    closed=value;
-    return;
-  }
-
-} // namespace orxonox
Index: code/trunk/src/network/PacketBuffer.h
===================================================================
--- code/trunk/src/network/PacketBuffer.h	(revision 3303)
+++ 	(revision )
@@ -1,80 +1,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Oliver Scheuss, (C) 2007
- *   Co-authors:
- *      ...
- *
- */
-
-//
-// C++ Interface: PacketBuffer
-//
-// Description:
-//
-//
-// Author:  Oliver Scheuss, (C) 2007
-//
-// Copyright: See COPYING file that comes with this distribution
-//
-//
-
-#ifndef _PacketBuffer_H__
-#define _PacketBuffer_H__
-
-#include "NetworkPrereqs.h"
-
-namespace orxonox
-{
-  struct _NetworkExport PacketEnvelope{
-    int length;
-    int data;
-  };
-
-  struct _NetworkExport QueueItem{
-    ENetEvent *event;
-    //ENetAddress address;
-    QueueItem *next;
-  };
-
-  class _NetworkExport PacketBuffer{
-  public:
-    PacketBuffer();
-    bool isEmpty();
-    bool isClosed();
-    void setClosed(bool value);
-    void print();
-    // pops a packet from the queue
-    //ENetPacket *pop();
-    //ENetPacket *pop(ENetAddress &address);
-    ENetEvent *pop();
-    // pushs a packet to the queue
-    bool push(ENetEvent *ev);
-
-  private:
-    QueueItem *first;
-    QueueItem *last;
-    bool closed;
-  };
-
-} //namespace
-#endif /* _PacketBuffer_H__ */
Index: code/trunk/src/network/Server.cc
===================================================================
--- code/trunk/src/network/Server.cc	(revision 3303)
+++ code/trunk/src/network/Server.cc	(revision 3304)
@@ -48,4 +48,5 @@
 #include "core/Clock.h"
 #include "core/ObjectList.h"
+#include "core/Executor.h"
 #include "packet/Chat.h"
 #include "packet/ClassID.h"
@@ -68,12 +69,10 @@
   */
   Server::Server() {
-    timeSinceLastUpdate_=0;
-    gamestates_ = new GamestateManager();
+    this->timeSinceLastUpdate_=0;
   }
 
   Server::Server(int port){
     this->setPort( port );
-    timeSinceLastUpdate_=0;
-    gamestates_ = new GamestateManager();
+    this->timeSinceLastUpdate_=0;
   }
 
@@ -86,6 +85,5 @@
     this->setPort( port );
     this->setBindAddress( bindAddress );
-    timeSinceLastUpdate_=0;
-    gamestates_ = new GamestateManager();
+    this->timeSinceLastUpdate_=0;
   }
 
@@ -94,6 +92,4 @@
   */
   Server::~Server(){
-    if(gamestates_)
-      delete gamestates_;
   }
 
@@ -138,15 +134,24 @@
   */
   void Server::update(const Clock& time) {
+    // receive incoming packets
     Connection::processQueue();
-    gamestates_->processGamestates();
-    //this steers our network frequency
-    timeSinceLastUpdate_+=time.getDeltaTime();
-    if(timeSinceLastUpdate_>=NETWORK_PERIOD)
+    
+    if ( ClientInformation::hasClients() )
     {
-      timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
-      updateGamestate();
+      // process incoming gamestates
+      GamestateManager::processGamestates();
+      
+      // send function calls to clients
       FunctionCallManager::sendCalls();
-    }
-    sendPackets(); // flush the enet queue
+      
+      //this steers our network frequency
+      timeSinceLastUpdate_+=time.getDeltaTime();
+      if(timeSinceLastUpdate_>=NETWORK_PERIOD)
+      {
+        timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
+        updateGamestate();
+      }
+      sendPackets(); // flush the enet queue
+    }
   }
 
@@ -175,8 +180,8 @@
   */
   void Server::updateGamestate() {
-//     if( ClientInformation::getBegin()==NULL )
+    if( ClientInformation::getBegin()==NULL )
       //no client connected
-//       return;
-    gamestates_->update();
+      return;
+    GamestateManager::update();
     COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
     //std::cout << "updated gamestate, sending it" << std::endl;
@@ -197,38 +202,36 @@
   */
   bool Server::sendGameState() {
-    COUT(5) << "Server: starting function sendGameState" << std::endl;
-    ClientInformation *temp = ClientInformation::getBegin();
-    bool added=false;
-    while(temp != NULL){
-      if( !(temp->getSynched()) ){
-        COUT(5) << "Server: not sending gamestate" << std::endl;
-        temp=temp->next();
-        if(!temp)
-          break;
-        //think this works without continue
-        continue;
-      }
-      COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
-      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
-      int gid = temp->getGamestateID(); //get gamestate id
-      int cid = temp->getID(); //get client id
-      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
-      packet::Gamestate *gs = gamestates_->popGameState(cid);
-      if(gs==NULL){
-        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
-        temp = temp->next();
-        continue;
-      }
-      //std::cout << "adding gamestate" << std::endl;
-      gs->setClientID(cid);
-      if ( !gs->send() ){
-        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
-        temp->addFailure();
-      }else
-        temp->resetFailures();
-      added=true;
-      temp=temp->next();
-      // gs gets automatically deleted by enet callback
-    }
+//     COUT(5) << "Server: starting function sendGameState" << std::endl;
+//     ClientInformation *temp = ClientInformation::getBegin();
+//     bool added=false;
+//     while(temp != NULL){
+//       if( !(temp->getSynched()) ){
+//         COUT(5) << "Server: not sending gamestate" << std::endl;
+//         temp=temp->next();
+//         if(!temp)
+//           break;
+//         continue;
+//       }
+//       COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
+//       COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
+//       int cid = temp->getID(); //get client id
+//       packet::Gamestate *gs = GamestateManager::popGameState(cid);
+//       if(gs==NULL){
+//         COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
+//         temp = temp->next();
+//         continue;
+//       }
+//       //std::cout << "adding gamestate" << std::endl;
+//       gs->setClientID(cid);
+//       if ( !gs->send() ){
+//         COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
+//         temp->addFailure();
+//       }else
+//         temp->resetFailures();
+//       added=true;
+//       temp=temp->next();
+//       // gs gets automatically deleted by enet callback
+//     }
+    GamestateManager::sendGamestates();
     return true;
   }
@@ -324,5 +327,5 @@
   void Server::disconnectClient( ClientInformation *client ){
     ServerConnection::disconnectClient( client );
-    gamestates_->removeClient(client);
+    GamestateManager::removeClient(client);
 // inform all the listeners
     ObjectList<ClientConnectionListener>::iterator listener = ObjectList<ClientConnectionListener>::begin();
Index: code/trunk/src/network/Server.h
===================================================================
--- code/trunk/src/network/Server.h	(revision 3303)
+++ code/trunk/src/network/Server.h	(revision 3304)
@@ -34,4 +34,5 @@
 #include "core/CorePrereqs.h"
 #include "Host.h"
+#include "GamestateManager.h"
 #include "ServerConnection.h"
 
@@ -43,5 +44,5 @@
   * It implements all functions necessary for a Server
   */
-  class _NetworkExport Server : public Host, public ServerConnection{
+  class _NetworkExport Server : public Host, public ServerConnection, public GamestateManager{
   public:
     Server();
@@ -63,5 +64,5 @@
     unsigned int shipID(){return 0;}
     unsigned int playerID(){return 0;}
-
+    
     void addClient(ENetEvent *event);
     bool createClient(int clientID);
@@ -75,7 +76,4 @@
     void syncClassid(unsigned int clientID);
 
-    GamestateManager *gamestates_;
-
-
     float timeSinceLastUpdate_;
   };
Index: code/trunk/src/network/packet/ClassID.cc
===================================================================
--- code/trunk/src/network/packet/ClassID.cc	(revision 3303)
+++ code/trunk/src/network/packet/ClassID.cc	(revision 3304)
@@ -142,5 +142,5 @@
     classname = temp+2*sizeof(uint32_t);
     id=ClassByString( std::string((const char*)classname) );
-    COUT(0) << "processing classid: " << networkID << " name: " << classname << " id: " << id << std::endl;
+    COUT(3) << "processing classid: " << networkID << " name: " << classname << " id: " << id << std::endl;
     if(id==NULL){
       COUT(0) << "Recieved a bad classname" << endl;
Index: code/trunk/src/network/packet/FunctionIDs.cc
===================================================================
--- code/trunk/src/network/packet/FunctionIDs.cc	(revision 3303)
+++ code/trunk/src/network/packet/FunctionIDs.cc	(revision 3304)
@@ -132,5 +132,5 @@
     stringsize = *(uint32_t*)(temp+sizeof(uint32_t));
     functionname = temp+2*sizeof(uint32_t);
-    COUT(0) << "processing functionid: " << networkID << " name: " << functionname << std::endl;
+    COUT(3) << "processing functionid: " << networkID << " name: " << functionname << std::endl;
     NetworkFunctionBase::setNetworkID((const char*)functionname, networkID);
     temp += 2*sizeof(uint32_t) + stringsize;
Index: code/trunk/src/network/synchronisable/NetworkCallbackManager.cc
===================================================================
--- code/trunk/src/network/synchronisable/NetworkCallbackManager.cc	(revision 3303)
+++ code/trunk/src/network/synchronisable/NetworkCallbackManager.cc	(revision 3304)
@@ -44,6 +44,6 @@
     if (it != callbackSet_.end())
     {
-      delete (*it);
       callbackSet_.erase(it);
+      delete cb;
     }
   }
Index: code/trunk/src/network/synchronisable/Synchronisable.cc
===================================================================
--- code/trunk/src/network/synchronisable/Synchronisable.cc	(revision 3303)
+++ code/trunk/src/network/synchronisable/Synchronisable.cc	(revision 3304)
@@ -96,10 +96,10 @@
       if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer()))
         deletedObjects_.push(objectID);
-      // delete all Synchronisable Variables from syncList ( which are also in stringList )
-      for(std::vector<SynchronisableVariableBase*>::iterator it = syncList.begin(); it!=syncList.end(); it++)
-        delete (*it);
-      syncList.clear();
-      stringList.clear();
-    }
+    }
+    // delete all Synchronisable Variables from syncList ( which are also in stringList )
+    for(std::vector<SynchronisableVariableBase*>::iterator it = syncList.begin(); it!=syncList.end(); it++)
+      delete (*it);
+    syncList.clear();
+    stringList.clear();
     std::map<uint32_t, Synchronisable*>::iterator it;
     it = objectMap_.find(objectID);
@@ -247,6 +247,8 @@
       return 0;
     uint32_t tempsize = 0;
+#ifndef NDEBUG
     if (this->classID==0)
       COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
+#endif
 
     if (this->classID == static_cast<uint32_t>(-1))
Index: code/trunk/src/network/synchronisable/Synchronisable.h
===================================================================
--- code/trunk/src/network/synchronisable/Synchronisable.h	(revision 3303)
+++ code/trunk/src/network/synchronisable/Synchronisable.h	(revision 3304)
@@ -43,8 +43,4 @@
 #include "NetworkCallback.h"
 
-/*#define REGISTERDATA(varname, ...) \
-    registerVariable(static_cast<void*>(&varname), sizeof(varname), DATA, __VA_ARGS__)
-#define REGISTERSTRING(stringname, ...) \
-    registerVariable(&stringname, stringname.length()+1, STRING, __VA_ARGS__)*/
 
 namespace orxonox
Index: code/trunk/src/network/synchronisable/SynchronisableSpecialisations.cc
===================================================================
--- code/trunk/src/network/synchronisable/SynchronisableSpecialisations.cc	(revision 3303)
+++ code/trunk/src/network/synchronisable/SynchronisableSpecialisations.cc	(revision 3304)
@@ -38,20 +38,13 @@
 namespace orxonox{
   
-//   template <> void Synchronisable::registerVariable( const std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
-//   {
-//     if (bidirectional)
-//       syncList.push_back(new SynchronisableVariableBidirectional<const std::string>(variable, mode, cb));
-//     else
-//       syncList.push_back(new SynchronisableVariable<const std::string>(variable, mode, cb));
-//     stringList.push_back(syncList.back());
-//   }
-  
   template <> void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
   {
+    SynchronisableVariableBase* sv;
     if (bidirectional)
-      syncList.push_back(new SynchronisableVariableBidirectional<const std::string>(variable, mode, cb));
+      sv = new SynchronisableVariableBidirectional<const std::string>(variable, mode, cb);
     else
-      syncList.push_back(new SynchronisableVariable<const std::string>(variable, mode, cb));
-    stringList.push_back(syncList.back());
+      sv = new SynchronisableVariable<const std::string>(variable, mode, cb);
+    syncList.push_back(sv);
+    stringList.push_back(sv);
   }
   
@@ -73,8 +66,4 @@
     registerVariable(variable.y, mode, cb, bidirectional);
   }
-//   template <> void Synchronisable::registerVariable( Vector2& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional)
-//   {
-//     registerVariable( (const ColourValue&)variable, mode, cb, bidirectional);
-//   }
   
   template <> void Synchronisable::registerVariable( const Vector3& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional)
Index: code/trunk/src/network/synchronisable/SynchronisableVariable.h
===================================================================
--- code/trunk/src/network/synchronisable/SynchronisableVariable.h	(revision 3303)
+++ code/trunk/src/network/synchronisable/SynchronisableVariable.h	(revision 3304)
@@ -117,5 +117,8 @@
   {
     if (this->callback_ != 0)
+    {
       NetworkCallbackManager::deleteCallback(this->callback_); //safe call for deletion
+      // this is neccessary because for example for a Vector3 all 3 components of the vector use the same callback
+    }
   }
 
Index: code/trunk/src/orxonox/LevelManager.h
===================================================================
--- code/trunk/src/orxonox/LevelManager.h	(revision 3303)
+++ code/trunk/src/orxonox/LevelManager.h	(revision 3304)
@@ -34,4 +34,5 @@
 #include <cassert>
 #include <list>
+#include <string>
 #include "core/OrxonoxClass.h"
 
Index: code/trunk/src/orxonox/gamestates/GSDedicated.cc
===================================================================
--- code/trunk/src/orxonox/gamestates/GSDedicated.cc	(revision 3303)
+++ code/trunk/src/orxonox/gamestates/GSDedicated.cc	(revision 3304)
@@ -42,5 +42,5 @@
 #include <boost/bind.hpp>
 
-#ifndef ORXONOX_PLATFORM_WINDOWS
+#ifdef ORXONOX_PLATFORM_UNIX
 #include <termios.h>
 #endif
@@ -58,5 +58,4 @@
         : GameState(params)
         , server_(0)
-        , timeSinceLastUpdate_(0)
         , closeThread_(false)
         , cleanLine_(true)
@@ -65,6 +64,4 @@
         , cursorY_(0)
     {
-        this->commandLine_ = new unsigned char[MAX_COMMAND_LENGTH];
-//         memset( this->commandLine_, 0, MAX_COMMAND_LENGTH );
     }
 
@@ -96,5 +93,5 @@
         
         closeThread_ = true;
-#ifndef ORXONOX_PLATFORM_WINDOWS
+#ifdef ORXONOX_PLATFORM_UNIX
         std::cout << "\033[0G\033[K";
         std::cout.flush();
@@ -102,5 +99,7 @@
         delete this->originalTerminalSettings_;
 #endif
-        //inputThread_->join();
+        COUT(0) << "Press enter to end the game..." << std::endl;
+        inputThread_->join();
+        delete this->inputThread_;
 
         GameMode::setHasServer(false);
@@ -109,16 +108,5 @@
     void GSDedicated::update(const Clock& time)
     {
-        timeSinceLastUpdate_ += time.getDeltaTime();
-        if (timeSinceLastUpdate_ >= NETWORK_PERIOD)
-        {
-            timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;
-            server_->update(time);
-        }
-        else
-        {
-            msleep(static_cast<unsigned int>((NETWORK_PERIOD - timeSinceLastUpdate_)*1000));
-            msleep(static_cast<unsigned int>(NETWORK_PERIOD*1000)); // NOTE: this is to throttle the non-network framerate
-//            COUT(0) << "sleeping for " << static_cast<int>((NETWORK_PERIOD - timeSinceLastUpdate_) * 1000 * 1000) << " usec" << endl;
-        }
+        server_->update(time);
         processQueue();
         printLine();
@@ -127,4 +115,6 @@
     void GSDedicated::inputThread()
     {
+        this->commandLine_ = new unsigned char[MAX_COMMAND_LENGTH];
+//         memset( this->commandLine_, 0, MAX_COMMAND_LENGTH );
         unsigned char c;
         unsigned int  escapeChar=0;
@@ -194,5 +184,6 @@
                             std::cout << endl << CommandExecutor::hint( std::string((const char*)this->commandLine_,inputIterator_) ) << endl;
                             strncpy(reinterpret_cast<char*>(this->commandLine_), CommandExecutor::complete( std::string(reinterpret_cast<char*>(this->commandLine_),inputIterator_) ).c_str(), MAX_COMMAND_LENGTH);
-                            inputIterator_ = strlen((const char*)this->commandLine_);
+                            this->inputIterator_ = strlen((const char*)this->commandLine_);
+                            this->cursorX_ = this->inputIterator_;
                             break;
                         }
@@ -207,9 +198,11 @@
             }
         }
+
+        delete[] this->commandLine_;
     }
     
     void GSDedicated::printLine()
     {
-#ifndef ORXONOX_PLATFORM_WINDOWS
+#ifdef ORXONOX_PLATFORM_UNIX
         // set cursor to the begining of the line and erase the line
         std::cout << "\033[0G\033[K";
@@ -251,5 +244,5 @@
     void GSDedicated::setTerminalMode()
     {
-#ifndef ORXONOX_PLATFORM_WINDOWS
+#ifdef ORXONOX_PLATFORM_UNIX
         termios new_settings;
      
@@ -268,5 +261,5 @@
     void GSDedicated::resetTerminalMode()
     {
-#ifndef ORXONOX_PLATFORM_WINDOWS
+#ifdef ORXONOX_PLATFORM_UNIX
         tcsetattr(0, TCSANOW, GSDedicated::originalTerminalSettings_);
 #endif
Index: code/trunk/src/orxonox/gamestates/GSDedicated.h
===================================================================
--- code/trunk/src/orxonox/gamestates/GSDedicated.h	(revision 3303)
+++ code/trunk/src/orxonox/gamestates/GSDedicated.h	(revision 3304)
@@ -66,5 +66,4 @@
         
         Server*                 server_;
-        float                   timeSinceLastUpdate_;
         
         boost::thread           *inputThread_;
Index: code/trunk/src/orxonox/gamestates/GSRoot.cc
===================================================================
--- code/trunk/src/orxonox/gamestates/GSRoot.cc	(revision 3303)
+++ code/trunk/src/orxonox/gamestates/GSRoot.cc	(revision 3304)
@@ -34,4 +34,5 @@
 #include "core/Game.h"
 #include "core/GameMode.h"
+#include "network/NetworkFunction.h"
 #include "tools/Timer.h"
 #include "interfaces/TimeFactorListener.h"
@@ -61,4 +62,5 @@
     GSRoot::~GSRoot()
     {
+        NetworkFunctionBase::destroyAllNetworkFunctions();
     }
 
Index: code/trunk/src/util/Math.cc
===================================================================
--- code/trunk/src/util/Math.cc	(revision 3303)
+++ code/trunk/src/util/Math.cc	(revision 3304)
@@ -136,11 +136,12 @@
                 return orxonox::Vector2(0, 1);
         }
-
-        float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1));
-
+        
+        float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1);
+        float sin_value = sqrt( 1 - cos_value*cos_value );
+        
         if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
-            return orxonox::Vector2(sin(angle), cos(angle));
-        else
-            return orxonox::Vector2(-sin(angle), cos(angle));
+            return orxonox::Vector2( sin_value, cos_value );
+        else
+            return orxonox::Vector2( -sin_value, cos_value );
     }
 
@@ -175,5 +176,8 @@
                 return orxonox::Vector2(0, 1);
         }
-        float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1));
+        //float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1));
+        
+        float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1);
+        float sin_value = sqrt( 1 - cos_value*cos_value );
 
         float distancelength = distance.length();
@@ -182,7 +186,7 @@
 
         if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
-            return orxonox::Vector2(sin(angle) * radius, cos(angle) * radius);
-        else
-            return orxonox::Vector2(-sin(angle) * radius, cos(angle) * radius);
+            return orxonox::Vector2( sin_value * radius, cos_value * radius);
+        else
+            return orxonox::Vector2( -sin_value * radius, cos_value * radius);
     }
 
Index: code/trunk/src/util/Serialise.h
===================================================================
--- code/trunk/src/util/Serialise.h	(revision 3303)
+++ code/trunk/src/util/Serialise.h	(revision 3304)
@@ -408,5 +408,5 @@
     template <> inline void loadAndIncrease( const std::string& variable, uint8_t*& mem )
     {
-        *(std::string*)( &variable ) = std::string((const char *)mem);
+        *(std::string*)( &variable ) = (const char *)mem;
         mem += variable.length()+1;
     }
@@ -414,5 +414,6 @@
     template <> inline bool checkEquality( const std::string& variable, uint8_t* mem )
     {
-        return std::string((const char*)mem)==variable;
+        //return std::string((const char*)mem)==variable;
+        return (const char*)mem==variable;
     }
 
