Fix bug and add test for it