implemented function for clening up memoryLeaks before exit