Implemented automatic for-loop optimization