implementing logic for farther and closer