Collapse duplicates and merge similars