added code to clean up memory