Stochastic version of PR #52
Stochastic version of PR #52