incorporated clarifications/fixes from wrb
replaced dev.off with try(dev.off(),silent=TRUE) to catch null device errors