refactoring and optimization