Fixing memory leak in summarizing functions