Refactor split_hash_by_keys method.