Added more error checking for input/output.