Add some documentation, refactor some code