Added safety checking.
Removed ugly hack, using map instead of unordered_map.