added better error handling for readbytes