added collapseDuplicates method