Markov function accepts n-gram perameter
updated from jessica's version