cleaned and commented version of markov generator