Added randomize and updated collapseDuplicates.