refactored handleRecursion