Fix bugs and added testing.