more efficient while loop