Small tweaks: tighten tolerances; remove for loops where possible.