refactored recursion method into smaller methods