change loop to recursion