Fixed a bunch of coverity issues