fixing nan error with modified ternary