refactor: writefile function