WIP: bugfix merge headers; additional tests; refactoring for better testability