Fix leaks/segfaults in mapreduce