improved the memory leak prevention