Added documentation and refactored