Break out closestCommonAncestor into a separate file, add tests