improved saving function