slight refactor of HMM code