Fixes never-ending while loop