add memory management from cm
credits to cm