Refactor #3985: Make undefined check more compatible