added documentation and refactoring