Refine code, avoid memory leak