use loop, not recursion