Safer (and generalised) namelist patching function