Added functionality so data will be saved after the app is terminated.