Index: src/Actions/FragmentationAction/FragmentationAutomationAction.cpp
===================================================================
--- src/Actions/FragmentationAction/FragmentationAutomationAction.cpp	(revision 27594e98143a33dbfb73bbd57d1e04ef63ad1cb8)
+++ src/Actions/FragmentationAction/FragmentationAutomationAction.cpp	(revision 4f056ee4f8f8ba61bbfbf5d3291ade39e1f70347)
@@ -372,29 +372,28 @@
 Action::state_ptr FragmentationFragmentationAutomationAction::performCall() {
   boost::asio::io_service io_service;
-  MPQCFragmentController mpqccontroller(io_service);
-  mpqccontroller.setHost(params.host.get());
-  mpqccontroller.setPort(params.port.get());
-  mpqccontroller.setLevel(params.level.get());
-  VMGFragmentController vmgcontroller(io_service);
-  vmgcontroller.setHost(params.host.get());
-  vmgcontroller.setPort(params.port.get());
-  VMGDebugGridFragmentController debugcontroller(io_service);
-  debugcontroller.setHost(params.host.get());
-  debugcontroller.setPort(params.port.get());
 
   // TODO: Have io_service run in second thread and merge with current again eventually
 
-  // Phase One: obtain ids
-  std::vector< boost::filesystem::path > jobfiles = params.jobfiles.get();
-  mpqccontroller.requestIds(jobfiles.size());
-
-  // Phase Two: create and add MPQCJobs
-  if (!mpqccontroller.addJobsFromFiles(params.executable.get().string(), jobfiles))
-    return Action::failure;
-
-  // Phase Three: calculate result
-  mpqccontroller.waitforResults(jobfiles.size());
+  size_t Exitflag = 0;
   std::map<JobId_t, MPQCData> fragmentData;
-  mpqccontroller.getResults(fragmentData);
+  {
+    MPQCFragmentController mpqccontroller(io_service);
+    mpqccontroller.setHost(params.host.get());
+    mpqccontroller.setPort(params.port.get());
+    mpqccontroller.setLevel(params.level.get());
+    // Phase One: obtain ids
+    std::vector< boost::filesystem::path > jobfiles = params.jobfiles.get();
+    mpqccontroller.requestIds(jobfiles.size());
+
+    // Phase Two: create and add MPQCJobs
+    if (!mpqccontroller.addJobsFromFiles(params.executable.get().string(), jobfiles))
+      return Action::failure;
+
+    // Phase Three: calculate result
+    mpqccontroller.waitforResults(jobfiles.size());
+    mpqccontroller.getResults(fragmentData);
+
+    Exitflag += mpqccontroller.getExitflag();
+  }
 
 #ifdef HAVE_VMG
@@ -404,5 +403,4 @@
 
   // obtain combined charge density
-  LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
   FragmentationChargeDensity summedChargeDensity(
       fragmentData,
@@ -410,25 +408,33 @@
   const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid();
 
+  LOG(1, "INFO: There are " << fragmentData.size() << " short-range and "
+      << full_sample.size() << " level-wise long-range jobs.");
+
   // Phase Four: obtain more ids
-  const size_t NoJobs = fragmentData.size()+full_sample.size();
-  vmgcontroller.requestIds(NoJobs);
-
-  // Phase Five: create VMGJobs
-  const size_t near_field_cells = params.near_field_cells.get();
-  if (!vmgcontroller.createLongRangeJobs(
-      fragmentData,
-      full_sample,
-      summedChargeDensity.getFragment(), near_field_cells))
-    return Action::failure;
-
-  // Phase Six: calculate result
-  vmgcontroller.waitforResults(NoJobs);
   std::map<JobId_t, VMGData> longrangeData;
-  vmgcontroller.getResults(longrangeData);
-  ASSERT( NoJobs == longrangeData.size(),
-      "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
-      +toString(full_sample)+" "
-      +toString(NoJobs)
-      +" and VMGresults "+toString(longrangeData.size())+" don't match.");
+  {
+    VMGFragmentController vmgcontroller(io_service);
+    vmgcontroller.setHost(params.host.get());
+    vmgcontroller.setPort(params.port.get());
+    const size_t NoJobs = fragmentData.size()+full_sample.size();
+    vmgcontroller.requestIds(NoJobs);
+
+    // Phase Five: create VMGJobs
+    const size_t near_field_cells = params.near_field_cells.get();
+    if (!vmgcontroller.createLongRangeJobs(
+        fragmentData,
+        full_sample,
+        summedChargeDensity.getFragment(), near_field_cells))
+      return Action::failure;
+
+    // Phase Six: calculate result
+    vmgcontroller.waitforResults(NoJobs);
+    vmgcontroller.getResults(longrangeData);
+    ASSERT( NoJobs == longrangeData.size(),
+        "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
+        +toString(full_sample.size())+"="+toString(NoJobs)
+        +" and VMGresults "+toString(longrangeData.size())+" don't match.");
+    Exitflag += vmgcontroller.getExitflag();
+  }
 
   // remove full solution corresponding to full_sample from map (must be highest ids), has to be treated extra
@@ -454,13 +460,19 @@
     LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
     printReceivedFullResults(results);
-
+  }
+
+  std::map<JobId_t, std::string> debugData;
+  {
     if (!full_sample.empty()) {
       // create debug jobs for each level to print the summed-up potential to vtk files
+      VMGDebugGridFragmentController debugcontroller(io_service);
+      debugcontroller.setHost(params.host.get());
+      debugcontroller.setPort(params.port.get());
       debugcontroller.requestIds(full_sample.size());
       if (!debugcontroller.createDebugJobs(full_sample))
         return Action::failure;
       debugcontroller.waitforResults(full_sample.size());
-      std::map<JobId_t, std::string> debugData;
       debugcontroller.getResults(debugData);
+      Exitflag += debugcontroller.getExitflag();
     }
   }
@@ -476,5 +488,4 @@
   }
 #endif
-  size_t Exitflag = vmgcontroller.getExitflag() + mpqccontroller.getExitflag();
 
   return (Exitflag == 0) ? Action::success : Action::failure;
