Fixed bogus test as NaNs cannot be compared
Fixed possible NPEs
Added more checking for NPEs