Making sure everything is finished when the next page is loaded