Fixed bugs and added lots of test