Some improvement in viterbi function