Reworked construction of distance table.