Rewrote recursion as decreasing list to make it easier to work with.