Fix issue #305 and add a test.