Moved some things out of the loop.