Fixed a bug and added a test