distance added. comments are move inside funtions and edited.