Moved some code around and fixed some comments.