added function to sort nids into unique sets