Improvements to the merging code

I reimplemented the merging algorithm with a cleaner interface. But further changes are likely due to the integration of conflict strategies