Cleanup and fixed memory leak