Added loop instead of recursion