Split and optimize loop