Refactoring some duplicate code