fix bug in writing