Fix errors detected with valgrind