Fix memory error detected by Valgrind