Move some parts of Viterbi algorighm to separate fuctions