Original A-Distance code in python (Tim Oates)