Fix issue #307 and add a test.