Fix bugs in save functions and change structure of the functions.