Fix write function