Switching to array_key_exists and fixing a critical bug