properly check if val is defined