Refactor SM2 quality calculation