changed the for loop length logic