Refactor some code and add documentation. Also removed most of the cout statements.