A rewrite of the cem code.