implemented markov chain; needs optimization