Index: /trunk/FACT++/src/InterpreterV8.cc
===================================================================
--- /trunk/FACT++/src/InterpreterV8.cc	(revision 14598)
+++ /trunk/FACT++/src/InterpreterV8.cc	(revision 14599)
@@ -34,6 +34,9 @@
     fThreadIds.clear();
 
-    if (fThreadId>=0)
-        V8::TerminateExecution(fThreadId);
+    if (fThreadId<0)
+        return;
+
+    V8::TerminateExecution(fThreadId);
+    fThreadId = -1;
 }
 
@@ -51,4 +54,5 @@
         // of four using a larger value, but this also means that the
         // JavaScript is locked for a longer time.
+        const Unlocker unlock;
         usleep(1000);
         return Undefined();
@@ -77,22 +81,19 @@
 
     return handle_scope.Close(script->Run());
-
-    //JsSleep(args[0]->Int32Value());
-    //return Undefined();
 }
 
 void InterpreterV8::ThreadTimeout(Persistent<Function> func, uint32_t ms)
 {
+    const Locker lock;
+
     if (fThreadId<0)
         return;
 
-    Locker lock;
-
-    const HandleScope handle_scope;
-
-    fGlobalContext->Enter();
-
     const int id = V8::GetCurrentThreadId();
     fThreadIds.insert(id);
+
+    const HandleScope handle_scope;
+
+    fGlobalContext->Enter();
 
     TryCatch exception;
@@ -891,8 +892,8 @@
 void InterpreterV8::JsHandleEvent(const EventImp &evt, uint64_t cnt, const string &service)
 {
+    const Locker locker;
+
     if (fThreadId<0)
         return;
-
-    Locker locker;
 
     const auto it = fReverseMap.find(service);
@@ -966,8 +967,8 @@
 void InterpreterV8::JsHandleState(const std::string &server, const State &state)
 {
+    const Locker locker;
+
     if (fThreadId<0)
         return;
-
-    Locker locker;
 
     auto it = fStateCallbacks.find(server);
@@ -1482,5 +1483,5 @@
 bool InterpreterV8::JsRun(const string &filename, const map<string, string> &map)
 {
-    Locker locker;
+    const Locker locker;
     fThreadId = V8::GetCurrentThreadId();
 
@@ -1571,11 +1572,11 @@
     // the signal to terminate. After they are all successfully
     // terminated, just to be sure... we lock again
-    Unlocker unlock;
-
-    for (auto it=fTimeout.begin(); it!=fTimeout.end(); it++)
-        it->join();
-    fTimeout.clear();
-
-    Locker lock2;
+    {
+        const Unlocker unlock;
+
+        for (auto it=fTimeout.begin(); it!=fTimeout.end(); it++)
+            it->join();
+        fTimeout.clear();
+    }
 
     // Now we can dispose all persistent handles from state callbacks
