working code, adding measure of homogenosity to reduce selective prssure when homogenous