Fixes #1228

This change fixes issue #1228 and adds related tests.