Refactoring nearest line calculation
Performance optimizations in find nearest line implementation