Cppcheck performance fixes

I ran cppcheck through the code with the performance option and fixed most of the issues with this pull request.

Have fun reviewing! ;)