speeded up the while loop