Mark Schmidt's SAG code