Backport of a9cc8dad64ba01ab0a37f65227b2e63a4b343a80 which addresses #455.