Updated recursion logic