Improve thread safety with additional checks and a synchronized map.