Refactoring unique index code