fixed bug failing under valgrind