Code cleanup and memory leak
Tried to fix the memory leak, but not sure how.