diff --git a/relational_gui/guihandler.py b/relational_gui/guihandler.py
index 5eeec66..2c4a252 100644
--- a/relational_gui/guihandler.py
+++ b/relational_gui/guihandler.py
@@ -37,7 +37,6 @@ class relForm(QtWidgets.QMainWindow):
         self.undo = None  # UndoQueue for queries
         self.selectedRelation = None
         self.ui = maingui.Ui_MainWindow()
-        self.qcounter = 1  # Query counter
         self.user_interface = UserInterface()
         self.history_current_line = None
 
@@ -79,8 +78,7 @@ class relForm(QtWidgets.QMainWindow):
 
     def next_history(self):
         if self.ui.lstHistory.currentRow() + 1 == self.ui.lstHistory.count() and self.history_current_line:
-            self.ui.txtResult.setText(self.history_current_line[0])
-            self.ui.txtQuery.setText(self.history_current_line[1])
+            self.ui.txtQuery.setText(self.history_current_line)
             self.history_current_line = None
         elif self.history_current_line:
             self.ui.lstHistory.setCurrentRow(self.ui.lstHistory.currentRow()+1)
@@ -88,11 +86,13 @@ class relForm(QtWidgets.QMainWindow):
 
     def prev_history(self):
         if self.history_current_line is None:
-            self.history_current_line = (self.ui.txtResult.text(), self.ui.txtQuery.text())
-            self.resumeHistory(self.ui.lstHistory.currentItem())
+            self.history_current_line = self.ui.txtQuery.text()
+
+            if not self.ui.lstHistory.currentItem().text() != self.ui.txtQuery.text():
+                self.ui.lstHistory.setCurrentRow(self.ui.lstHistory.currentRow()-1)
         elif self.ui.lstHistory.currentRow() > 0:
             self.ui.lstHistory.setCurrentRow(self.ui.lstHistory.currentRow()-1)
-            self.resumeHistory(self.ui.lstHistory.currentItem())
+        self.resumeHistory(self.ui.lstHistory.currentItem())
 
     def add_shortcuts(self, shortcuts):
         for widget,shortcut,slot in shortcuts:
@@ -143,10 +143,12 @@ class relForm(QtWidgets.QMainWindow):
         '''Performs all the possible optimizations on the query'''
         self.undo = self.ui.txtQuery.text()  # Storing the query in undo list
 
-        query = self.ui.txtQuery.text()
+        res_rel,query = self.user_interface.split_query(self.ui.txtQuery.text(),None)
         try:
             result = optimizer.optimize_all(
                 query, self.user_interface.relations)
+            if res_rel:
+                result = '%s = %s' % (res_rel, result)
             self.ui.txtQuery.setText(result)
         except Exception as e:
             self.error(e)
@@ -154,9 +156,8 @@ class relForm(QtWidgets.QMainWindow):
     def resumeHistory(self, item):
         if item is None:
             return
-        itm = item.text().split(' = ', 1)
-        self.ui.txtResult.setText(itm[0])
-        self.ui.txtQuery.setText(itm[1])
+        itm = item.text()
+        self.ui.txtQuery.setText(itm)
 
     def _run_multiline(self):
         query = self.ui.txtMultiQuery.toPlainText()
@@ -182,8 +183,7 @@ class relForm(QtWidgets.QMainWindow):
                 return self._run_multiline()
 
             # Single line query
-            query = self.ui.txtQuery.text()
-            res_rel = self.ui.txtResult.text()  # result relation's name
+            res_rel,query = self.user_interface.split_query(self.ui.txtQuery.text())
 
             try:
                 self.selectedRelation = self.user_interface.execute(query, res_rel)
@@ -193,18 +193,10 @@ class relForm(QtWidgets.QMainWindow):
                 return self.error(e)
 
             # Adds to history
-            item = u'%s = %s' % (
-                self.ui.txtResult.text(),
-                self.ui.txtQuery.text()
-            )
             hitem = QtWidgets.QListWidgetItem(None, 0)
-            hitem.setText(item)
+            hitem.setText(self.ui.txtQuery.text())
             self.ui.lstHistory.addItem(hitem)
             self.ui.lstHistory.setCurrentItem(hitem)
-
-            self.qcounter += 1
-            # Sets the result relation name to none
-            self.ui.txtResult.setText(u"_last%d" % self.qcounter)
         finally:
             self.setMultiline(self.multiline)
 
diff --git a/relational_gui/maingui.ui b/relational_gui/maingui.ui
index 82516b5..e89a429 100644
--- a/relational_gui/maingui.ui
+++ b/relational_gui/maingui.ui
@@ -261,29 +261,6 @@
            <property name="spacing">
             <number>0</number>
            </property>
-           <item>
-            <widget class="QLineEdit" name="txtResult">
-             <property name="sizePolicy">
-              <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
-               <horstretch>0</horstretch>
-               <verstretch>0</verstretch>
-              </sizepolicy>
-             </property>
-             <property name="text">
-              <string>_last1</string>
-             </property>
-            </widget>
-           </item>
-           <item>
-            <widget class="QLabel" name="label">
-             <property name="text">
-              <string>=</string>
-             </property>
-             <property name="buddy">
-              <cstring>txtQuery</cstring>
-             </property>
-            </widget>
-           </item>
            <item>
             <widget class="QLineEdit" name="txtQuery"/>
            </item>
@@ -1161,8 +1138,8 @@
    <slot>showAbout()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>97</x>
-     <y>74</y>
+     <x>95</x>
+     <y>444</y>
     </hint>
     <hint type="destinationlabel">
      <x>79</x>
@@ -1178,7 +1155,7 @@
    <hints>
     <hint type="sourcelabel">
      <x>78</x>
-     <y>132</y>
+     <y>484</y>
     </hint>
     <hint type="destinationlabel">
      <x>99</x>
@@ -1193,8 +1170,8 @@
    <slot>unloadRelation()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>677</x>
-     <y>396</y>
+     <x>516</x>
+     <y>490</y>
     </hint>
     <hint type="destinationlabel">
      <x>773</x>
@@ -1209,8 +1186,8 @@
    <slot>saveRelation()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>783</x>
-     <y>322</y>
+     <x>631</x>
+     <y>422</y>
     </hint>
     <hint type="destinationlabel">
      <x>760</x>
@@ -1225,8 +1202,8 @@
    <slot>loadRelation()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>677</x>
-     <y>322</y>
+     <x>516</x>
+     <y>422</y>
     </hint>
     <hint type="destinationlabel">
      <x>753</x>
@@ -1241,8 +1218,8 @@
    <slot>printRelation(QListWidgetItem*)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>708</x>
-     <y>110</y>
+     <x>633</x>
+     <y>328</y>
     </hint>
     <hint type="destinationlabel">
      <x>643</x>
@@ -1257,8 +1234,8 @@
    <slot>showAttributes(QListWidgetItem*)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>615</x>
-     <y>182</y>
+     <x>633</x>
+     <y>384</y>
     </hint>
     <hint type="destinationlabel">
      <x>510</x>
@@ -1353,8 +1330,8 @@
    <slot>editRelation()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>783</x>
-     <y>359</y>
+     <x>631</x>
+     <y>456</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1385,8 +1362,8 @@
    <slot>newRelation()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>677</x>
-     <y>359</y>
+     <x>516</x>
+     <y>456</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1449,12 +1426,12 @@
    <slot>clear()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>556</x>
-     <y>608</y>
+     <x>251</x>
+     <y>256</y>
     </hint>
     <hint type="destinationlabel">
-     <x>432</x>
-     <y>578</y>
+     <x>221</x>
+     <y>286</y>
     </hint>
    </hints>
   </connection>
@@ -1465,8 +1442,8 @@
    <slot>execute()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>556</x>
-     <y>646</y>
+     <x>251</x>
+     <y>256</y>
     </hint>
     <hint type="destinationlabel">
      <x>795</x>
@@ -1481,12 +1458,12 @@
    <slot>clear()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>696</x>
-     <y>700</y>
+     <x>310</x>
+     <y>491</y>
     </hint>
     <hint type="destinationlabel">
-     <x>606</x>
-     <y>699</y>
+     <x>260</x>
+     <y>490</y>
     </hint>
    </hints>
   </connection>
@@ -1497,12 +1474,12 @@
    <slot>clear()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>556</x>
-     <y>556</y>
+     <x>394</x>
+     <y>453</y>
     </hint>
     <hint type="destinationlabel">
-     <x>490</x>
-     <y>481</y>
+     <x>395</x>
+     <y>418</y>
     </hint>
    </hints>
   </connection>
@@ -1513,8 +1490,8 @@
    <slot>optimize()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>263</x>
-     <y>556</y>
+     <x>186</x>
+     <y>453</y>
     </hint>
     <hint type="destinationlabel">
      <x>130</x>
@@ -1529,8 +1506,8 @@
    <slot>undoOptimize()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>409</x>
-     <y>556</y>
+     <x>296</x>
+     <y>453</y>
     </hint>
     <hint type="destinationlabel">
      <x>544</x>
@@ -1545,8 +1522,8 @@
    <slot>execute()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>786</x>
-     <y>700</y>
+     <x>394</x>
+     <y>491</y>
     </hint>
     <hint type="destinationlabel">
      <x>797</x>
@@ -1561,8 +1538,8 @@
    <slot>execute()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>606</x>
-     <y>699</y>
+     <x>260</x>
+     <y>490</y>
     </hint>
     <hint type="destinationlabel">
      <x>796</x>
@@ -1577,8 +1554,8 @@
    <slot>resumeHistory(QListWidgetItem*)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>282</x>
-     <y>465</y>
+     <x>384</x>
+     <y>418</y>
     </hint>
     <hint type="destinationlabel">
      <x>297</x>
@@ -1593,8 +1570,8 @@
    <slot>addProduct()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>183</y>
+     <x>46</x>
+     <y>87</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1609,8 +1586,8 @@
    <slot>addDifference()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>220</y>
+     <x>90</x>
+     <y>87</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1625,8 +1602,8 @@
    <slot>addUnion()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>257</y>
+     <x>46</x>
+     <y>121</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1641,8 +1618,8 @@
    <slot>addIntersection()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>294</y>
+     <x>90</x>
+     <y>121</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1657,8 +1634,8 @@
    <slot>addDivision()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>331</y>
+     <x>46</x>
+     <y>223</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1673,8 +1650,8 @@
    <slot>addOuter()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>479</y>
+     <x>90</x>
+     <y>155</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1689,8 +1666,8 @@
    <slot>addOLeft()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>405</y>
+     <x>46</x>
+     <y>189</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1705,8 +1682,8 @@
    <slot>addORight()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>442</y>
+     <x>90</x>
+     <y>189</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1721,8 +1698,8 @@
    <slot>addJoin()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>368</y>
+     <x>46</x>
+     <y>155</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1737,8 +1714,8 @@
    <slot>addProjection()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>516</y>
+     <x>90</x>
+     <y>257</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1753,8 +1730,8 @@
    <slot>addSelection()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>553</y>
+     <x>46</x>
+     <y>257</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1769,8 +1746,8 @@
    <slot>addRename()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>590</y>
+     <x>46</x>
+     <y>291</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1785,8 +1762,8 @@
    <slot>addArrow()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>67</x>
-     <y>627</y>
+     <x>90</x>
+     <y>291</y>
     </hint>
     <hint type="destinationlabel">
      <x>399</x>
@@ -1801,8 +1778,8 @@
    <slot>newSession()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>736</x>
-     <y>378</y>
+     <x>631</x>
+     <y>490</y>
     </hint>
     <hint type="destinationlabel">
      <x>796</x>