Reorganize the code some and add comments.