changes copied from PR#256

See #256 (and discussion at #312)