corrected issue with numeric stability