Changed things to log probabilities to prevent numerical underflow