Fix type stability issue
Fix potential NaN handling issue