Switch from O(n^2) to O(n) duplicate detection
Merge branch 'optimize' into hotfix