Improve uniqueness logic