minor refactoring and optimization