added savedata function. Not fully working yet