Optimize loops