Several bugs, which together fix #246.  See the comment in the new regression test for details.
Merge branch 'master' into torepr-pp