several valgrind-detected fixes