distance table read, and a little cleanup code