Use isnan(3) to check for NaN
fix memory leak found by valgrind
Merge pull request #1 from swolchok/master

Use isnan(3) to check for NaN