refactoring the markov