added trial randomization and fixed key dependence