Fix error handling when output file is specified