fix bug in writeVar