Internal restructure stochastic

Restructure loops over trajectories so that it will be easy to parallelize.