Added memory cleanup.
Merge branch 'master' into whole_pattern