for-loop optimization