Fix segmentation faults when output directory doesn't exist or is not writable