Math and randomness improvements