Clean up code a bit and add some comments.