a little reformatting
#! fixed
now it can handle 'nan' case