Refactor write_file ~ changing flow and adding custom buffer size