Refactor to avoid code repetition.