Significantly simplified the mapping code