Add some documentation, clean up code a little