Improve the robustness of the common prefix calculation to work as
intended in all known cases.