Refactor the code in save_to_disk to make it more re-usable