improved algorithms for distance calculations; associative arrays; added functions