Performance optimizations and fixed memory leak.