collapseDuplicates method added