Minor optimization

Move loop invariant code.