fixed up the input and output, and checked for file open errors.