Fix errors/warnings reported by valgrind