save to file refactor