Corrected a bug and added randomness