Fixed error on while statement inside recursion