Modified HMM code to output results into file