Updated save function