Updated I/O handling to properly deal with errors and passing that error code up the stack