refactor f.write