changed generate_pai() and min_hash() to reduce total number of processing.