added documentation and refactored some code