fixed recursion logic