changed while loop to counter