More merge test cases

(And bug fixes and refactoring)