memory leak fixes & some minor improvements

memory leak fixes & some minor improvements