Coverity fix and better consistency checks