valgrind test & fix some bugs