Refactored map code