added test and some refactoring