Merging should stop when oplog is running out
Major code refactor

Previous code has become less readable. This patch did quite some
changes to improve it.