moved linkage calculation code into separate functions