some refactoring and added tests