MarkovEngine class and a minimal refactorization