Fix nil check

Testing against nil isn't reliable -- use typeof != nil instead.