Fixed bugs and added new testcases