Fix underflow issues by performing computation in log space