fix memory leak and others