Extended script to higher order markov chains