Solve some memory leak problems