Optimization Pass

Loops, distance checks, and comparison operations have been optimized
and/or implemented where necessary.