Fix coverity fixes

Fix bugs caused by the massive coverity scan bug hunt.