Improve logic of CMS