added memory clean up code