added a map.contains check to avoid potential null pointers