Tested all test cases and fixed a few bugs.