Refactor the common logic up one level