Code cleanup and add comments.