Code fixes

Various code fixes, mostly discovered by MSVC code static analyzer .
Intent is exactly what this commit does :)