Code fixes

Various code fixes, mostly discovered by MSVC code static analyzer .
In case of low memory Log will fail too with very high probability