added extra check on whether output dirs exist.