check if val is defined before overwriting