fixed bug and added test.