added code to prevent memory leak