Refactor some code and document a little more