You might want to assing your macros (@LDOMAIN) to variables in the beginning of the script. This way you can check for a correct value just in that one place.

Oh, yes, and consolidate, consolidate, consolidate. My rule of thumb is, if I do something at least twice, it justifies a UDF. Using UDFs will also simplify debugging because you have less code to check where an error might be introduced.
_________________________
There are two types of vessels, submarines and targets.