Added more error checking and changed output_dir