Added documentation and cleaned up code a bit.