More robust SG generation. Only remaining issues are R-3m and R-3c.