fixed top while loop bug