fixed write function for real and test it