Added some docs and cleaned up the code a bit.