Index: /trunk/FACT++/scripts/Main.js
===================================================================
--- /trunk/FACT++/scripts/Main.js	(revision 19516)
+++ /trunk/FACT++/scripts/Main.js	(revision 19517)
@@ -816,5 +816,6 @@
     console.out("  IRQ:  "+cmd);
     console.out("  Time: "+time);
-    console.out("  User: "+user);
+    if (user)
+        console.out("  User: "+user);
 
     if (cmd.toUpperCase()=="RELOADSOURCES")
@@ -829,5 +830,40 @@
     // This will end a run in progress as if it where correctly stopped
     if (dim.state("MCP").name=="TakingData")
-        dim.send("MCP/STOP");
+    {
+        if (irq.toUpperCase()=="PREPARE")
+        {
+            // Avoid stopping the trigger preparing for a ToO
+            // this would result in the FEEDBACK going from InProgress to Connected
+            // and thus the threshold settings cannot be reused
+            if (dim.state("FAD_CONTROL").name=="RunInProgress")
+                dim.send("FAD_CONTROL/CLOSE_OPEN_FILES");
+        }
+        else
+            dim.send("MCP/STOP");
+    }
+
+    if (irq.toUpperCase()=="PREPARE")
+    {
+        triggerReloadSources = true;
+        dim.send("DRIVE_CONTROL/STOP");
+        irq = undefined;
+    }
+
+    /*
+	if (fDimFTM.state()==FTM::State::kTriggerOn)
+	{
+            Message("Stopping FTM");
+	    Dim::SendCommandNB("FTM_CONTROL/STOP_TRIGGER");
+	}
+
+        // FIXME: Do step 2 only when FTM is stopped
+        if (fDimFAD.state()==FAD::State::kConnected || fDimFAD.state()==FAD::State::kRunInProgress)
+        {
+            if (fDimFAD.state()==FAD::State::kRunInProgress)
+                Dim::SendCommandNB("FAD_CONTROL/CLOSE_OPEN_FILES");
+        }
+
+        return GetCurrentState();
+     */
 
     // This will stop a rate scan in progress
@@ -879,4 +915,7 @@
         return false;
 
+    while (irq.toUpperCase()=="PREPARE")
+        v8.sleep(10);
+
     if (irq.toUpperCase()=="RESCHEDULE")
     {
@@ -903,12 +942,4 @@
     {
         Shutdown();
-        return true;
-    }
-
-    if (irq.toUpperCase()=="PREPARE")
-    {
-        dim.log("Preparing for ToO observation.");
-        dim.send("DRIVE_CONTROL/STOP");
-        dim.wait("DRIVE_CONTROL", "Initialized", 15000);
         return true;
     }
@@ -1594,5 +1625,6 @@
         // Now we are 'OnTrack', so we can ramp to nominal voltage
         // and wait for the feedback to get stable
-        service_feedback.voltageOn();
+        //if (!obs[sub].grb || dim.state("FEEDBACK").name!="InProgress")
+            service_feedback.voltageOn();
         if (!obs[sub].grb)
             service_feedback.waitForVoltageOn();
@@ -1617,4 +1649,5 @@
             if (dim.state("LID_CONTROL").name!="Open")
             {
+                dim.log("Waiting for an open lid.");
                 dim.wait("LID_CONTROL", "Open", 30000);
                 service_feedback.waitForVoltageOn();
