some refactoring to facilitate testing
some refactoring to facilitate testing