Factored out duplicate-checking code.