adjusted loop to use grid size variables in iteration