better error checking for write (#3)