Code fixes

Various code fixes, mostly discovered by MSVC code static analyzer .
It's copy/paste problem. Look at few lines up.
Without this '-1' it can address beyond end of the array