fix #747

added a test as well. reviews of the test are welcom