BUG: fix a corner case and add a test