refactoring, more tests, some documentation