Updated write function