updated with recursion