Index: /trunk/FACT++/src/InterpreterV8.cc
===================================================================
--- /trunk/FACT++/src/InterpreterV8.cc	(revision 14631)
+++ /trunk/FACT++/src/InterpreterV8.cc	(revision 14632)
@@ -27,6 +27,6 @@
 v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateLocal;
 v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateSky;
-v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateMoon;
-v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateDatabase;
+//v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateMoon;
+//v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateDatabase;
 
 
@@ -147,5 +147,5 @@
     const HandleScope handle_scope;
 
-    fGlobalContext->Enter();
+    func->CreationContext()->Enter();
 
     TryCatch exception;
@@ -301,6 +301,4 @@
     if (rc.index>-256 && !date.IsEmpty())
         obj->Set(String::New("time"),  date);
-
-    //obj->Set(String::New("toString"),  String::New(("[Object state "+string(*str)+":"+to_string(rc.index)+"]").c_str()));
 
     return handle_scope.Close(obj->NewInstance());
@@ -679,13 +677,11 @@
         HandleScope handle_scope;
 
+        //if (!args.IsConstructCall())
+        //    return Constructor(fTemplateDatabase, args);
+
         Database *db = new Database(*String::Utf8Value(args[0]));
         fDatabases.push_back(db);
 
         Handle<Object> This = args.This();
-        if (!args.IsConstructCall())
-            This = fTemplateDatabase->PrototypeTemplate()->NewInstance();
-        if (This.IsEmpty())
-            return Undefined();
-
         This->Set(String::New("user"),     String::New(db->user.c_str()), ReadOnly);
         This->Set(String::New("server"),   String::New(db->server.c_str()), ReadOnly);
@@ -756,5 +752,5 @@
     }
 
-    args.Holder()->Set(String::New("isOpen"), Boolean::New(false), ReadOnly);
+    args.This()->Set(String::New("isOpen"), Boolean::New(false), ReadOnly);
 
     return handle_scope.Close(Boolean::New(JsUnsubscribe(*str)));
@@ -999,9 +995,7 @@
     const HandleScope handle_scope;
 
-    fGlobalContext->Enter();
-
-    Handle<Object> obj = it->second;
-    if (obj.IsEmpty())
-        return;
+    Persistent<Object> obj = it->second;
+
+    obj->CreationContext()->Enter();
 
     const Handle<String> onchange = String::New("onchange");
@@ -1040,5 +1034,5 @@
 }
 
-Handle<Value> InterpreterV8::OnChangeSet(Local<String> prop, Local< Value > value, const AccessorInfo &)
+Handle<Value> InterpreterV8::OnChangeSet(Local<String> prop, Local<Value> value, const AccessorInfo &)
 {
     // Returns the value if the setter intercepts the request. Otherwise, returns an empty handle.
@@ -1053,5 +1047,5 @@
 
     if (value->IsFunction())
-        fStateCallbacks[server] = Persistent<Value>::New(value);
+        fStateCallbacks[server] = Persistent<Object>::New(value->ToObject());
 
     return Handle<Value>();
@@ -1074,10 +1068,7 @@
     }
 
-    if (it->second.IsEmpty() || !it->second->IsFunction())
-        return;
+    it->second->CreationContext()->Enter();
 
     const HandleScope handle_scope;
-
-    fGlobalContext->Enter();
 
     // -------------------------------------------------------------------
@@ -1101,5 +1092,5 @@
 
     Handle<Value> args[] = { obj->NewInstance() };
-    Handle<Function> fun = Handle<Function>::Cast(it->second);
+    Handle<Function> fun = Handle<Function>(Function::Cast(*it->second));
     fun->Call(fun, 1, args);
 
@@ -1355,9 +1346,6 @@
     // ----------------------------
 
-    Handle<Object> This = args.This();
     if (!args.IsConstructCall())
-        This = fTemplateMoon->PrototypeTemplate()->NewInstance();
-    if (This.IsEmpty())
-        return Undefined();
+        return Constructor(args);
 
     Handle<Function> function =
@@ -1366,4 +1354,5 @@
         return Undefined();
 
+    Handle<Object> This = args.This();
     This->Set(String::New("ra"),      Number::New(equ.ra/15),  ReadOnly);
     This->Set(String::New("dec"),     Number::New(equ.dec),    ReadOnly);
@@ -1392,9 +1381,6 @@
     HandleScope handle_scope;
 
-    Handle<Object> This = args.This();
     if (!args.IsConstructCall())
-        This = fTemplateSky->PrototypeTemplate()->NewInstance();
-    if (This.IsEmpty())
-        return Undefined();
+        return Constructor(args);
 
     Handle<Function> function =
@@ -1403,4 +1389,5 @@
         return Undefined();
 
+    Handle<Object> This = args.This();
     This->Set(String::New("ra"),      Number::New(ra),  ReadOnly);
     This->Set(String::New("dec"),     Number::New(dec), ReadOnly);
@@ -1420,5 +1407,4 @@
         return ThrowException(String::New("Third argument must be a Date."));
 
-
     const double zd = args[0]->NumberValue();
     const double az = args[1]->NumberValue();
@@ -1431,9 +1417,6 @@
     HandleScope handle_scope;
 
-    Handle<Object> This = args.This();
     if (!args.IsConstructCall())
-        This = fTemplateLocal->PrototypeTemplate()->NewInstance();
-    if (This.IsEmpty())
-        return Undefined();
+        return Constructor(args);
 
     Handle<Function> function =
@@ -1442,4 +1425,5 @@
         return Undefined();
 
+    Handle<Object> This = args.This();
     This->Set(String::New("zd"),    Number::New(zd), ReadOnly);
     This->Set(String::New("az"),    Number::New(az), ReadOnly);
@@ -1600,4 +1584,15 @@
 //                                  CORE
 // ==========================================================================
+
+Handle<Value> InterpreterV8::Constructor(/*Handle<FunctionTemplate> T,*/ const Arguments &args)
+{
+    Handle<Value> argv[args.Length()];
+
+    for (int i=0; i<args.Length(); i++)
+        argv[i] = args[i];
+
+    return args.Callee()->NewInstance(args.Length(), argv);
+}
+
 
 void InterpreterV8::AddFormatToGlobal() const
@@ -1706,6 +1701,4 @@
     global->Set(String::New("Database"), db, ReadOnly);
 
-    fTemplateDatabase = db;
-
 #ifdef HAVE_NOVA
     Handle<FunctionTemplate> sky = FunctionTemplate::New(ConstructorSky);
@@ -1725,10 +1718,9 @@
     fTemplateLocal = loc;
     fTemplateSky   = sky;
-    fTemplateMoon  = moon;
 #endif
 
     // Persistent
-    fGlobalContext = Context::New(NULL, global);
-    if (fGlobalContext.IsEmpty())
+    Persistent<Context> context = Context::New(NULL, global);
+    if (context.IsEmpty())
     {
         //printf("Error creating context\n");
@@ -1736,11 +1728,11 @@
     }
 
-    Context::Scope scope(fGlobalContext);
+    Context::Scope scope(context);
 
     Handle<Array> args = Array::New(map.size());
     for (auto it=map.begin(); it!=map.end(); it++)
         args->Set(String::New(it->first.c_str()), String::New(it->second.c_str()));
-    fGlobalContext->Global()->Set(String::New("$"),   args, ReadOnly);
-    fGlobalContext->Global()->Set(String::New("arg"), args, ReadOnly);
+    context->Global()->Set(String::New("$"),   args, ReadOnly);
+    context->Global()->Set(String::New("arg"), args, ReadOnly);
 
     //V8::ResumeProfiler();
@@ -1812,5 +1804,5 @@
     fStates.clear();
 
-    fGlobalContext.Dispose();
+    context.Dispose();
 
     JsEnd(filename);
Index: /trunk/FACT++/src/InterpreterV8.h
===================================================================
--- /trunk/FACT++/src/InterpreterV8.h	(revision 14631)
+++ /trunk/FACT++/src/InterpreterV8.h	(revision 14632)
@@ -27,6 +27,4 @@
     std::set<int> fThreadIds;
 
-    v8::Persistent<v8::Context> fGlobalContext;
-
     // A loookup table which allows to indentify the
     // the JavaScript object corrsponding to the
@@ -36,5 +34,5 @@
 
     // Lookup table for the callbacks in cases of state changes
-    std::map<std::string, v8::Persistent<v8::Value>>  fStateCallbacks;
+    std::map<std::string, v8::Persistent<v8::Object>> fStateCallbacks;
 
     // List of all timeout threads
@@ -46,6 +44,6 @@
     static v8::Handle<v8::FunctionTemplate> fTemplateLocal;
     static v8::Handle<v8::FunctionTemplate> fTemplateSky;
-    static v8::Handle<v8::FunctionTemplate> fTemplateMoon;
-    static v8::Handle<v8::FunctionTemplate> fTemplateDatabase;
+    //static v8::Handle<v8::FunctionTemplate> fTemplateMoon;
+    //static v8::Handle<v8::FunctionTemplate> fTemplateDatabase;
 
 #ifdef HAVE_SQL
@@ -88,4 +86,5 @@
 
     static double GetDataMember(const v8::Arguments &args, const char *name);
+    static v8::Handle<v8::Value> Constructor(const v8::Arguments &args);
 
     static v8::Handle<v8::Value> LocalToString(const v8::Arguments &args);
