Added bunch of test cases and cleaned up a few bugs.