Fixed bug and added tests