fixed while-loop bug