fixed bug and added tests