Tweaked factoring to avoid redundant containsKey key.