Several test cases and fixes