Various CID fixes v2

Updated version of pull request #276:
- dropped one patch (5df2952)
- add some more fixes (6) for CID issues