Reorganize the code a little and fix some comments.