Use recursion instead of while loop