refactor map code