Fix issue#506
Wrote tests for issue #506