fixed 'nan' appearing in the output