Revert "Revert "Revert "Update MAP code from upstream."""

This reverts commit 14e5c729eb31ae8708ccaedc14dcaff86bd7cc1e.