Rewriting Recursion using a stack.