Refactored map_foreachinmap

Also map_foreachininstance.
Added some real documentation.