Refactoring, Coverity fixes, added Coverity badge