First version of saving function