hmm implementation