Fix a bug and add more tests