Added documentation and refactored code somewhat.