Refactor saveMap function