Fix some bugs found by cppcheck