Change from flat map to map when possible