Fix some invalid writes in valgrind