porting over gap statistic code