refactored to reverse-while, no recursion strategy