Merge some code from CM upstream.