Coverage fixes

This supersedes PR #628 by adding a fixing a few more issues and adds tests.