this version runs and returns correct solution
At this stage only the iterative part needs to be done