modified save function