finished code for saving