small cleanup and some memory leak fixing