Added documentation and cleaned up some code.