Added documentation and refactored some parts of the code