corrected write function.