factor out logic for selecting preferred keypairs