remove some code duplication and package refactoring