copied in code from cm