Refactor some duplicate code.