Move some parts of Viterbi algorithm to separate fuctions