refactored common functionality