Refactor undefined checks