Fix recursion causing memory to run out