bug discovered by valgrind