Bunch of fixes for coverity issues