Cleaner implementation of migrate_keys().