DRYing up the dedup/sorting into its own method