add 'distance' function, and little other changes.