Fix problems detected by valgrind