merge repeated undefined checks into function