diff --git a/window.cpp b/window.cpp
index e8d0b2270e322f4ba99dcc8212bbb167e8e94381..f78ca48bfe789df621346fd74002a4dfe75f0364 100644
--- a/window.cpp
+++ b/window.cpp
@@ -14,12 +14,12 @@ Window::Window(QWidget *parent)
     connect(m_button, SIGNAL(clicked()), this, SLOT(solveButtonClicked()));
     m_button->setGeometry(1200, height() - 30, width() - 1200, 30);
     m_slider = new QSlider(this);
-    m_slider->setRange(0,1000);
+    m_slider->setRange(0, 1000);
     m_slider->setOrientation(Qt::Horizontal);
     m_slider->setValue(m_delay);
     m_slider->setGeometry(1200, height() - 60, width() - 1200, 30);
-    connect(m_slider,SIGNAL(valueChanged(int)),this,SLOT(setValue(int)));
-    
+    connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
+
     int dx = 0;
     int dy = 0;
     for (int x = 0; x < 9; x++)
@@ -36,8 +36,8 @@ Window::Window(QWidget *parent)
                 dy = 4;
             if (y >= 6)
                 dy = 8;
-            m_cell[x+y*9].setParent(this); 
-            m_cell[x+y*9].setGeometry(x * 100 + dx, y * 100 + dy, 100, 100);
+            m_cell[x + y * 9].setParent(this);
+            m_cell[x + y * 9].setGeometry(x * 100 + dx, y * 100 + dy, 100, 100);
             /* code */
         }
     }
@@ -46,23 +46,23 @@ Window::Window(QWidget *parent)
     {
         for (int y = 0; y < 9; y++)
         {
-            for (int d = 0; d < 9; d++)  //rows and columns
+            for (int d = 0; d < 9; d++) // rows and columns
             {
-                connect(&m_cell[x+y*9], SIGNAL(update(int)), &m_cell[x+d*9], SLOT(removeOption(int)));
-                connect(&m_cell[x+y*9], SIGNAL(update(int)), &m_cell[d+y*9], SLOT(removeOption(int)));
+                connect(&m_cell[x + y * 9], SIGNAL(update(int)), &m_cell[x + d * 9], SLOT(removeOption(int)));
+                connect(&m_cell[x + y * 9], SIGNAL(update(int)), &m_cell[d + y * 9], SLOT(removeOption(int)));
 
-                connect(&m_cell[x+y*9], SIGNAL(undo(int)), &m_cell[x+d*9], SLOT(addOption(int)));
-                connect(&m_cell[x+y*9], SIGNAL(undo(int)), &m_cell[d+y*9], SLOT(addOption(int)));
+                connect(&m_cell[x + y * 9], SIGNAL(undo(int)), &m_cell[x + d * 9], SLOT(addOption(int)));
+                connect(&m_cell[x + y * 9], SIGNAL(undo(int)), &m_cell[d + y * 9], SLOT(addOption(int)));
             }
             int a = x / 3 * 3;
             int b = y / 3 * 3;
-            for (int x1 = a; x1 < a + 3; x1++) //square
+            for (int x1 = a; x1 < a + 3; x1++) // square
             {
                 for (int y1 = b; y1 < b + 3; y1++)
                 {
-                    connect(&m_cell[x+y*9], SIGNAL(update(int)), &m_cell[x1+y1*9], SLOT(removeOption(int)));
+                    connect(&m_cell[x + y * 9], SIGNAL(update(int)), &m_cell[x1 + y1 * 9], SLOT(removeOption(int)));
 
-                    connect(&m_cell[x+y*9], SIGNAL(undo(int)), &m_cell[x1+y1*9], SLOT(addOption(int)));
+                    connect(&m_cell[x + y * 9], SIGNAL(undo(int)), &m_cell[x1 + y1 * 9], SLOT(addOption(int)));
                 }
             }
         }
@@ -71,56 +71,73 @@ Window::Window(QWidget *parent)
 
 void Window::solveButtonClicked()
 {
-    // std::array<Cell,81> start = m_cell;
-    std::list<Cell*> hist;
+    std::list<History *> hist;
+    bool end = false;
     srand(time(NULL));
     while (1)
     {
-        std::vector<Cell *> b; //vector of cells with least entropy
-        int minEtropy = 10; 
-        for (int x = 0; x < 9; x++) //loop through all cells and fill list b --> this could be optimzed 
+        if (end)
         {
-            for (int y = 0; y < 9; y++)
+            hist.back()->cell->un();
+            if (hist.back()->index.size() == 0)
             {
-                if (m_cell[x+y*9].collapsed)
-                    continue;
-                else if (m_cell[x+y*9].possibleStates < minEtropy)
-                {
-                    minEtropy = m_cell[x+y*9].possibleStates;
-                    b.clear();
-                    b.push_back(&m_cell[x+y*9]);
-                }
-                else if (m_cell[x+y*9].possibleStates == minEtropy)
-                {
-                    b.push_back(&m_cell[x+y*9]);
-                }
+                hist.pop_back();
+                continue;
             }
+            end = false;
+            int choiceNr = rand() % hist.back()->index.size();         // choose a random number;
+            hist.back()->cell->collapse(hist.back()->index[choiceNr]); // collapse cell with chosen number;
+            std::vector<int>::iterator it;
+            it = hist.back()->index.begin() + choiceNr;
+            hist.back()->index.erase(it);
         }
-        if (b.size() == 0) //if b is empty -> solved 
+        else
         {
-            qInfo("Solved!!");
-            return;
-        }
-        else if(minEtropy ==0){
-            while (hist.size()>0)
+            std::vector<Cell *> b; // vector of cells with least entropy
+            int minEtropy = 10;
+            for (int x = 0; x < 9; x++) // loop through all cells and fill list b --> this could be optimzed
             {
-                hist.back()->un();
-                hist.pop_back();
+                for (int y = 0; y < 9; y++)
+                {
+                    if (m_cell[x + y * 9].collapsed)
+                        continue;
+                    else if (m_cell[x + y * 9].possibleStates < minEtropy)
+                    {
+                        minEtropy = m_cell[x + y * 9].possibleStates;
+                        b.clear();
+                        b.push_back(&m_cell[x + y * 9]);
+                    }
+                    else if (m_cell[x + y * 9].possibleStates == minEtropy)
+                    {
+                        b.push_back(&m_cell[x + y * 9]);
+                    }
+                }
+            }
+            if (b.size() == 0) // if b is empty -> solved
+            {
+                qInfo("Solved!!");
+                return;
             }
-            return;
-            //solveButtonClicked();
+            else if (minEtropy == 0)
+            {
+                qInfo("backtracking");
+                end = true;
+                continue;
+            }
+
+            int choiceCell = rand() % b.size();                  // choose a random cell
+            int choiceNr = rand() % b[choiceCell]->index.size(); // choose a random number;
+            hist.push_back(new History(b[choiceCell], b[choiceCell]->index, choiceNr));
+            b[choiceCell]->collapse(b[choiceCell]->index[choiceNr]); // collapse that random cell with chosen number;
         }
-        int choiceCell = rand() % b.size(); //choose a random cell 
-        int choiceNr = rand() % b[choiceCell]->index.size(); //choose a random number;
-        b[choiceCell]->collapse(b[choiceCell]->index[choiceNr]); //collapse that random cell with chosen number;
-        hist.push_back(b[choiceCell]);
-        repaint(); //draw cells again
+        repaint(); // draw cells again
         QThread::msleep(m_delay);
     }
     // qInfo("%d---:)");
 }
 
-void Window::setValue(int s){
+void Window::setValue(int s)
+{
     m_delay = s;
     return;
 }
\ No newline at end of file
diff --git a/window.h b/window.h
index 3127c7e2675b3575f488d648501efce57c402e64..e0adef72fdf7196e3862005096b0205e3bf67bf4 100644
--- a/window.h
+++ b/window.h
@@ -11,14 +11,30 @@ class Window : public QWidget
     Q_OBJECT
 public:
     explicit Window(QWidget *parent = nullptr);
+
 private:
     QPushButton *m_buttonQuit;
     QPushButton *m_button;
     QPushButton *grid[9][9];
     QProgressBar *m_progressBar;
     QSlider *m_slider;
-    std::array<Cell,81> m_cell;
-    int m_delay =300;
+    std::array<Cell, 81> m_cell;
+    int m_delay = 300;
+    struct History
+    {
+        Cell *cell;
+        std::vector<int> index;
+        History(Cell *c, std::vector<int> in,int choice)
+        {
+            cell = c;
+            for(int i=0;i<in.size();i++){
+                if(i == choice)
+                    continue;
+                index.push_back(in[i]);
+            }
+        }
+    };
+
 signals:
     void counterReached();
 private slots: