Hoist this logic out of the loop, since it's used after the end of the loop