Index: /trunk/FACT++/src/InterpreterV8.cc
===================================================================
--- /trunk/FACT++/src/InterpreterV8.cc	(revision 15138)
+++ /trunk/FACT++/src/InterpreterV8.cc	(revision 15139)
@@ -148,8 +148,4 @@
     TryCatch exception;
 
-    //const Handle<Script> sleep = Script::Compile(String::New("v8.sleep();"), String::New("internal"));
-    //if (sleep.IsEmpty())
-    //    return Undefined();
-
     Handle<Function> func = Handle<Function>::Cast(args[1]);
 
@@ -163,13 +159,8 @@
         const Handle<Value> rc = func->Call(func, args.Length()-2, argv);
         if (!rc.IsEmpty() && !rc->IsUndefined())
-            return rc;
+            return handle_scope.Close(rc);
 
         if (!null && Time()-t>=boost::posix_time::milliseconds(abs(timeout)))
             break;
-
-        // We cannot sleep directly because we have to give control back to
-        // JavaScript ever now and then. This also allows us to catch
-        // exceptions, either from the preemption or ConvertEvent
-        //sleep->Run();
 
         // Theoretically, the CPU usage can be reduced by maybe a factor
@@ -1220,8 +1211,4 @@
     HandleScope handle_scope;
 
-    //const Handle<Script> sleep = Script::Compile(String::New("v8.sleep();"), String::New("internal"));
-    //if (sleep.IsEmpty())
-    //    return Undefined();
-
     const Handle<String> data   = String::New("data");
     const Handle<String> object = String::New("obj");
@@ -1244,24 +1231,20 @@
 
             // Protect against the return of an exception
-            if (!val.IsEmpty() && val->IsObject())
+            if (val->IsObject())
             {
-                if (!named)
+                const Handle<Object> event = val->ToObject();
+                const Handle<Value>  obj   = event->Get(named?object:data);
+                if (!obj.IsEmpty())
                 {
-                    const Handle<Object> event = val->ToObject();
-                    const Handle<Value>  obj   = event->Get(data);
-
-                    // No names (no 'obj'), but 'data'
-                    if (!obj.IsEmpty() && !obj->IsUndefined())
-                        return handle_scope.Close(val);
-                }
-                else
-                {
-                    const Handle<Object> event = val->ToObject();
-                    const Handle<Value>  obj   = event->Get(object);
-
-                    if (!obj.IsEmpty() && obj->IsObject())
+                    if (!named)
+                    {
+                        // No names (no 'obj'), but 'data'
+                        if (!obj->IsUndefined())
+                            return handle_scope.Close(val);
+                    }
+                    else
                     {
                         // Has names and data was received?
-                        if (obj->ToObject()->GetOwnPropertyNames()->Length()>0)
+                        if (obj->IsObject() && obj->ToObject()->GetOwnPropertyNames()->Length()>0)
                             return handle_scope.Close(val);
                     }
@@ -1275,9 +1258,4 @@
         if (!null && Time()-t>=boost::posix_time::milliseconds(abs(timeout)))
             break;
-
-        // We cannot sleep directly because we have to give control back to
-        // JavaScript ever now and then. This also allows us to catch
-        // exceptions, either from the preemption or ConvertEvent
-        //sleep->Run();
 
         // Theoretically, the CPU usage can be reduced by maybe a factor
@@ -1687,5 +1665,5 @@
 
     if (!args.IsConstructCall())
-        return Constructor(args);
+        return handle_scope.Close(Constructor(args));
 
     Handle<Function> function =
@@ -1722,5 +1700,5 @@
 
     if (!args.IsConstructCall())
-        return Constructor(args);
+        return handle_scope.Close(Constructor(args));
 
     Handle<Function> function =
@@ -1758,5 +1736,5 @@
 
     if (!args.IsConstructCall())
-        return Constructor(args);
+        return handle_scope.Close(Constructor(args));
 
     Handle<Function> function =
