Refactor recursion handling to allow recursive calls with a limited call stack size