Extract common method for save_as and close