some optimizations and distance limit