Move recursion to iteration

to appease python's recursion limits.