refactor to remove some duplication