Moves startI/endI calculation outside of loop