Fix invalid memory shown by valgrind

This should be self-explanatory :)