Improved termination of recursion of solve()