Add an error-checking in write()