Modified for memory leak detection