Simplify & beautify the code by using `nan`

Simplify & beautify the code by using `nan`