Code cleanup and fixes from cppcheck