Fix bugs thanks to valgrind