Refactored common functionality into library