Fix issue #302 and add test