refactored slow implementation based on unordered_map