Fix more issues found via cppcheck

Fixing issues like:
- use static_cast instead of C-Style cast
- reduce scope of variables
- initialize some variables in constructor
- remove unused variables
- remove useless assignments 
- remove identical else branch
- fix warning about comparing int with bool
- pass const string by reference
- remove consecutive return/break statement