Got rid of recursion from the algorithm.