Clean up, add comments.  Fix potential bug.