using log probabilities to avoid overflow