Improved hmm trainer code