Refactoring, fix tests, coverage