Refactor fix for #154 and add a test.