Viterbi function improved