Correlation function code, forked from Beth Reid