Further work on issue #20 and related issues. Not finished.