Fix build and several coverity issues