refector code
fix memory usage bug