Stop possible overflows

Coverity scan caught several places that could have possible code overflows - fixed them.
Also cleaned-up some compiler warnings.