Fixes stack overflow bug in loop

Replaces the recursive call in the loop method with a while loop. As it was
written, loop would only run 1000 times then would raise a
RuntimeError: maximum recursion depth exceeded while calling a Python object.