some refactoring and modularization