fixed many subtle bugs in the stochastic model