refactoring for reducing code duplication.