pulling lilly's changes
fixed merge conflicts
free all the memories.