Refactor into library