fix the while loop at the end