Added some code to clean up memory.  Currently looking for a memory-related bug in expansion