Initial import of Mercier code, trimmed down to relevant distances