slight refactoring, added tests