Corrected logic error in distance function; fixed comments