Some more smaller fixes

Fixes include:
- reduce scope of some variables
- use static_cast instead of C-Style casts
- remove unused variables
- don't check unsigned variable against less than zero