rewrote the recursive function without using enumerate function. Wasn't stopping in iOS8