Code fixes

Various code fixes, mostly discovered by MSVC code static analyzer .
I think a log would make sense here too