Refactoring, add tests