Used log sum exp to fix underflow