Trying to fix while loop bug