Refactor and test for map_instances