refactoring for code re-use and generic functions