Fixing bad check for undefined and another possible error