fix & simplify mergeLines code, more tests