Fixed bug and added some tests.