Fix more memory leak and pointer issues