refactoring, added some tests