refactored recursion