Fix a bug and add a test.