For loop optimization