fix error in distance sorting function, added analytics