Modified write function