Limit was causing stack overflow with recursive function.
Tried to convert it into iterative.