Refactor recursion to own function.
Refactor recursion into iteration.