[FIX] Fixed memory leaks caught by valgrind.