fixed write function