Added test cases and initial fixes