more write file refactoring