Add some comments and clean up the code a bit