massive JS refactor and cleanup for the map