loop instead of recursion