refactor to use while-loop instead of recursion (b/c it's a bit silly to use both recursion and a stack, no?)