Add error checking for write operation