Fix memory leak and improve package during index build.