Replaced unordered_map with map, some small optimizations