bug fix and unit test for it