Just moved some code around to make it easier to find and add some comments.