Refactor maps code