Code fixes

Various code fixes, mostly discovered by MSVC code static analyzer .
Any particular reason for 20?

is it http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx BAD_UNIT ?