some refactoring and adding more tests