Refactor and comments

- comments on all methods
- reduce code duplication
- improve document structure