tests, documentation and some refactoring