moving isNaN check earlier