Cleanup the code and add comments.