Refactor to remove some replicated code