rewrite writing function

Change-Id: I7fb1246dd54c1636693e8ca979f20ad39074f7ff