For loop optimization

Optimizes the for loop