I am travelling now, so I will not be able to fix this until the next week.
The fix for this particular issue is simple, but the general tendency somewhat worries me --
too many IsNan checks might hurt performance... or maybe this effect will be insignificant
and is nothing to worry about.