refactor common save and save_as code into own function