Fixed a few potential mem leaks, pointed by valgrind.