Refactor some duplicated code.