some code refactoring and duplication deletion