cleaning up some code and improving notnan flag