memory leak fixes, cleanup during exit