refactor while loop and handling wrong input