Fixed issue #341 and added unit test