Refactor for recursion