Fix bugs found by cppcheck