Fix issue #306 and add test