move file save functionality to its own method