Add error checking when creating output file