diff --git a/transition_machine/src/ActionBank.cpp b/transition_machine/src/ActionBank.cpp
index a5ee86f36978ebae657de939b819bbdb0ac6ae2c..74121631d886add7b22afc823103d7790fcaba3e 100644
--- a/transition_machine/src/ActionBank.cpp
+++ b/transition_machine/src/ActionBank.cpp
@@ -676,192 +676,200 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
   else if(std::string(b1) == "EOS")
   {
     // Puting the EOS tag on the last element of the sentence.
-    auto apply0 = [b2](Config & c, Action::BasicAction &)
-      {
-        int b0 = c.getHead();
-        int s0 = c.stackTop();
-        simpleBufferWrite(c, ProgramParameters::sequenceDelimiterTape, ProgramParameters::sequenceDelimiter, s0-b0);
-      };
-    auto undo0 = [](Config & c, Action::BasicAction)
-      {
-        int b0 = c.getHead();
-        int s0 = c.stackTop();
-        simpleBufferWrite(c, ProgramParameters::sequenceDelimiterTape, "", s0-b0);
-      };
-    auto appliable0 = [](Config & c, Action::BasicAction &)
-      {
-        return !c.isFinal() && !c.stackEmpty();
-      };
-    Action::BasicAction basicAction0 =
-      {Action::BasicAction::Type::Write, "", apply0, undo0, appliable0};
+    {
+      auto apply = [b2](Config & c, Action::BasicAction &)
+        {
+          int b0 = c.getHead();
+          int s0 = c.stackTop();
+          simpleBufferWrite(c, ProgramParameters::sequenceDelimiterTape, ProgramParameters::sequenceDelimiter, s0-b0);
+        };
+      auto undo = [](Config & c, Action::BasicAction)
+        {
+          int b0 = c.getHead();
+          int s0 = c.stackTop();
+          simpleBufferWrite(c, ProgramParameters::sequenceDelimiterTape, "", s0-b0);
+        };
+      auto appliable = [](Config & c, Action::BasicAction &)
+        {
+          return !c.isFinal() && !c.stackEmpty();
+        };
+      Action::BasicAction basicAction =
+        {Action::BasicAction::Type::Write, "", apply, undo, appliable};
+
+      sequence.emplace_back(basicAction);
+    }
+
+    // Update the IDs of the words in the new sentence
+    {
+      auto apply = [](Config & c, Action::BasicAction &)
+        {
+          c.updateIdsInSequence();
+        };
+      auto undo = [](Config &, Action::BasicAction &)
+        {
+        };
+      auto appliable = [](Config &, Action::BasicAction &)
+        {
+          return true;
+        };
+      Action::BasicAction basicAction =
+        {Action::BasicAction::Type::Write, "", apply, undo, appliable};
 
-    sequence.emplace_back(basicAction0);
+      sequence.emplace_back(basicAction);
+    }
 
     // Chosing root of the sentence and attaching floating words to it.
-    auto apply = [](Config & c, Action::BasicAction & ba)
-      {
-        ba.data = "";
-        auto & govs = c.getTape("GOV");
-        auto & ids = c.getTape("ID");
-        int b0 = c.getHead();
-        int rootIndex = -1;
-        for (int i = c.stackSize()-1; i >= 0; i--)
+    {
+      auto apply = [](Config & c, Action::BasicAction & ba)
         {
-          auto s = c.stackGetElem(i);
-          if (util::split(ids.getRef(s-b0), '-').size() > 1)
-            continue;
-          if (util::split(ids.getRef(s-b0), '.').size() > 1)
-            continue;
-          if (govs.getHyp(s-b0).empty() || govs.getHyp(s-b0) == "0")
+          ba.data = "";
+          auto & govs = c.getTape("GOV");
+          auto & ids = c.getTape("ID");
+          int b0 = c.getHead();
+          int rootIndex = -1;
+          for (int i = c.stackSize()-1; i >= 0; i--)
           {
-            if (rootIndex == -1)
-              rootIndex = s;
-            else
+            auto s = c.stackGetElem(i);
+            if (util::split(ids.getRef(s-b0), '-').size() > 1)
+              continue;
+            if (util::split(ids.getRef(s-b0), '.').size() > 1)
+              continue;
+            if (govs.getHyp(s-b0).empty() || govs.getHyp(s-b0) == "0")
             {
-              simpleBufferWrite(c, "GOV", std::to_string(rootIndex-s), s-b0);
-              simpleBufferWrite(c, "LABEL", "_", s-b0);
-              ba.data += "+"+std::to_string(s-b0);
+              if (rootIndex == -1)
+                rootIndex = s;
+              else
+              {
+                simpleBufferWrite(c, "GOV", std::to_string(rootIndex-s), s-b0);
+                simpleBufferWrite(c, "LABEL", "_", s-b0);
+                ba.data += "+"+std::to_string(s-b0);
+              }
             }
           }
-        }
 
-        if (rootIndex == -1)
-        {
-          if (c.stackEmpty())
+          if (rootIndex == -1)
           {
-            c.printForDebug(stderr);
-            fprintf(stderr, "ERROR (%s) : no suitable candidate for root. Aborting.\n", ERRINFO);
-            exit(1);
-          }
-
-          rootIndex = c.stackGetElem(c.stackSize()-1);
-        }
+            if (c.stackEmpty())
+            {
+              c.printForDebug(stderr);
+              fprintf(stderr, "ERROR (%s) : no suitable candidate for root. Aborting.\n", ERRINFO);
+              exit(1);
+            }
 
-        simpleBufferWrite(c, "GOV", "0", rootIndex-b0);
-        simpleBufferWrite(c, "LABEL", "root", rootIndex-b0);
+            rootIndex = c.stackGetElem(c.stackSize()-1);
+          }
 
-        // Attaching floating words to new root
-        int sentenceEnd = b0;
-        auto & eos = c.getTape(ProgramParameters::sequenceDelimiterTape);
-        while (sentenceEnd >= 0 && eos[sentenceEnd-b0] != ProgramParameters::sequenceDelimiter)
-          sentenceEnd--;
-        int sentenceStart = std::max(0,sentenceEnd-1);
-        while (sentenceStart >= 0 && eos[sentenceStart-b0] != ProgramParameters::sequenceDelimiter)
-          sentenceStart--;
-        sentenceStart++;
+          simpleBufferWrite(c, "GOV", "0", rootIndex-b0);
+          simpleBufferWrite(c, "LABEL", "root", rootIndex-b0);
 
-        if (sentenceEnd < 0)
-        {
-          sentenceStart = 0;
-          sentenceEnd = eos.hypSize()-1;
-        }
+          // Attaching floating words to new root
+          int sentenceEnd = b0;
+          auto & eos = c.getTape(ProgramParameters::sequenceDelimiterTape);
+          while (sentenceEnd >= 0 && eos[sentenceEnd-b0] != ProgramParameters::sequenceDelimiter)
+            sentenceEnd--;
+          int sentenceStart = std::max(0,sentenceEnd-1);
+          while (sentenceStart >= 0 && eos[sentenceStart-b0] != ProgramParameters::sequenceDelimiter)
+            sentenceStart--;
+          sentenceStart++;
 
-        for (int i = sentenceStart; i <= sentenceEnd; i++)
-        {
-          if (util::split(ids.getRef(i-b0), '-').size() > 1)
-            continue;
-          if (util::split(ids.getRef(i-b0), '.').size() > 1)
-            continue;
-          if (govs.getHyp(i-b0).empty())
+          if (sentenceEnd < 0)
           {
-            simpleBufferWrite(c, "GOV", std::to_string(rootIndex-i), i-b0);
-            simpleBufferWrite(c, "LABEL", "_", i-b0);
-            ba.data += "+"+std::to_string(i-b0);           
+            sentenceStart = 0;
+            sentenceEnd = eos.hypSize()-1;
           }
-        }
 
-        // Delete the arcs from the previous sentence to the new sentence
-        for (int i = b0; i > c.stackTop(); i--)
-        {
-          try
+          for (int i = sentenceStart; i <= sentenceEnd; i++)
           {
-            if (std::stoi(govs[i-b0])+i <= c.stackTop())
+            if (util::split(ids.getRef(i-b0), '-').size() > 1)
+              continue;
+            if (util::split(ids.getRef(i-b0), '.').size() > 1)
+              continue;
+            if (govs.getHyp(i-b0).empty())
             {
-              simpleBufferWrite(c, "GOV", "", i-b0);
-              simpleBufferWrite(c, "LABEL", "", i-b0);
+              simpleBufferWrite(c, "GOV", std::to_string(rootIndex-i), i-b0);
+              simpleBufferWrite(c, "LABEL", "_", i-b0);
+              ba.data += "+"+std::to_string(i-b0);           
             }
           }
-          catch (std::exception &) {continue;}
-        }
-      };
-    auto undo = [](Config & c, Action::BasicAction & ba)
-      {
-        auto & govs = c.getTape("GOV");
-        int b0 = c.getHead();
-        for (int i = c.stackSize()-1; i >= 0; i--)
-        {
-          auto s = c.stackGetElem(i);
-          if (govs.getHyp(s-b0) == "0")
+
+          // Delete the arcs from the previous sentence to the new sentence
+          for (int i = b0; i > c.stackTop(); i--)
           {
-            simpleBufferWrite(c, "GOV", "", s-b0);
-            simpleBufferWrite(c, "LABEL", "", s-b0);
-            break;
+            try
+            {
+              if (std::stoi(govs[i-b0])+i <= c.stackTop())
+              {
+                simpleBufferWrite(c, "GOV", "", i-b0);
+                simpleBufferWrite(c, "LABEL", "", i-b0);
+              }
+            }
+            catch (std::exception &) {continue;}
           }
-        }
-        auto deps = util::split(ba.data, '+');
-        for (auto s : deps)
-          if (!s.empty())
+        };
+      auto undo = [](Config & c, Action::BasicAction & ba)
+        {
+          auto & govs = c.getTape("GOV");
+          int b0 = c.getHead();
+          for (int i = c.stackSize()-1; i >= 0; i--)
           {
-            simpleBufferWrite(c, "GOV", "", std::stoi(s));
-            simpleBufferWrite(c, "LABEL", "", std::stoi(s));
+            auto s = c.stackGetElem(i);
+            if (govs.getHyp(s-b0) == "0")
+            {
+              simpleBufferWrite(c, "GOV", "", s-b0);
+              simpleBufferWrite(c, "LABEL", "", s-b0);
+              break;
+            }
           }
-        ba.data.clear();
-      };
-    auto appliable = [](Config & c, Action::BasicAction &)
-      {
-        return !c.stackEmpty();
-      };
-    Action::BasicAction basicAction =
-      {Action::BasicAction::Type::Write, "", apply, undo, appliable};
+          auto deps = util::split(ba.data, '+');
+          for (auto s : deps)
+            if (!s.empty())
+            {
+              simpleBufferWrite(c, "GOV", "", std::stoi(s));
+              simpleBufferWrite(c, "LABEL", "", std::stoi(s));
+            }
+          ba.data.clear();
+        };
+      auto appliable = [](Config & c, Action::BasicAction &)
+        {
+          return !c.stackEmpty();
+        };
+      Action::BasicAction basicAction =
+        {Action::BasicAction::Type::Write, "", apply, undo, appliable};
 
-    sequence.emplace_back(basicAction);
+      sequence.emplace_back(basicAction);
+    }
 
     // Empty the stack.
-    auto apply4 = [](Config & c, Action::BasicAction & ba)
-      {
-        ba.data = "";
-        for (int i = c.stackSize()-1; i >= 0; i--)
+    {
+      auto apply = [](Config & c, Action::BasicAction & ba)
         {
-          auto s = c.stackGetElem(i);
-          ba.data += std::to_string(s) + " ";
-        }
-
-        while (!c.stackEmpty())
-          c.stackPop();
-      };
-    auto undo4 = [](Config & c, Action::BasicAction & ba)
-      {
-        auto elems = util::split(ba.data);
-        for (auto elem : elems)
-          if (!elem.empty())
-            c.stackPush(std::stoi(elem));
-        ba.data.clear();
-      };
-    auto appliable4 = [](Config & c, Action::BasicAction &)
-      {
-        return !c.isFinal() && !c.stackEmpty();
-      };
-    Action::BasicAction basicAction4 =
-      {Action::BasicAction::Type::Pop, "", apply4, undo4, appliable4};
-
-    sequence.emplace_back(basicAction4);
+          ba.data = "";
+          for (int i = c.stackSize()-1; i >= 0; i--)
+          {
+            auto s = c.stackGetElem(i);
+            ba.data += std::to_string(s) + " ";
+          }
 
-    // Update the IDs of the words in the new sentence
-    auto apply5 = [](Config & c, Action::BasicAction &)
-      {
-        c.updateIdsInSequence();
-      };
-    auto undo5 = [](Config &, Action::BasicAction &)
-      {
-      };
-    auto appliable5 = [](Config &, Action::BasicAction &)
-      {
-        return true;
-      };
-    Action::BasicAction basicAction5 =
-      {Action::BasicAction::Type::Write, "", apply5, undo5, appliable5};
+          while (!c.stackEmpty())
+            c.stackPop();
+        };
+      auto undo = [](Config & c, Action::BasicAction & ba)
+        {
+          auto elems = util::split(ba.data);
+          for (auto elem : elems)
+            if (!elem.empty())
+              c.stackPush(std::stoi(elem));
+          ba.data.clear();
+        };
+      auto appliable = [](Config & c, Action::BasicAction &)
+        {
+          return !c.isFinal() && !c.stackEmpty();
+        };
+      Action::BasicAction basicAction =
+        {Action::BasicAction::Type::Pop, "", apply, undo, appliable};
 
-    sequence.emplace_back(basicAction5);
+      sequence.emplace_back(basicAction);
+    }
   }
   else if(std::string(b1) == "BACK")
   {
diff --git a/transition_machine/src/Config.cpp b/transition_machine/src/Config.cpp
index 6a8b828d21a6559b42c0b08cbfbecd3c63ec4e87..83d05896fa95b9dd2d51ad7a21f8f103d7a39fcc 100644
--- a/transition_machine/src/Config.cpp
+++ b/transition_machine/src/Config.cpp
@@ -885,7 +885,7 @@ void Config::setGovsAsUD(bool ref)
 
 void Config::updateIdsInSequence()
 {
-  int sentenceEnd = getHead();
+  int sentenceEnd = stackHasIndex(0) ? stackGetElem(0) : getHead();
   auto & eos = getTape(ProgramParameters::sequenceDelimiterTape);
   auto & ids = getTape("ID");
   while (sentenceEnd >= 0 && eos[sentenceEnd-getHead()] != ProgramParameters::sequenceDelimiter)
@@ -895,10 +895,15 @@ void Config::updateIdsInSequence()
     sentenceStart--;
   sentenceStart++;
 
+  if (ProgramParameters::debug)
+    fprintf(stderr, "Updating IDS of sentence. Start=%d End=%d\n", sentenceStart, sentenceEnd);
+
   if (sentenceEnd < 0)
   {
     sentenceStart = 0;
     sentenceEnd = eos.hypSize()-1;
+    if (ProgramParameters::debug)
+      fprintf(stderr, "Correcting : Start=%d End=%d\n", sentenceStart, sentenceEnd);
   }
 
   int curId = 1;
@@ -923,5 +928,8 @@ void Config::updateIdsInSequence()
     ids.setHyp(i-getHead(), std::to_string(curId)+"-"+std::to_string(curId+multiWordSize));
     digitIndex = 1;
   }
+
+  if (ProgramParameters::debug)
+    fprintf(stderr, "Done updating IDS.\n");
 }