code refactoring

- various performance improvements
- code formatting
- documentation fix