Issue #507 fix

For issue #507 

- [x] Add failing test case
- [x] Fix bug