Move critical section out of large loop.