used valgrind to fix some bugs in LM.cpp