Add chalksy's new fix, add some undefined checks