added randomization and sorting