Updated to use map, tiny speedup
Refactoring