Code fixes

Various code fixes, mostly discovered by MSVC code static analyzer .
if this happens we have a critical error we most likely won't recover from. At least we should log something, right?