Fix minor bugs and implement test cases