fix last doc write function