Added `trim_urls` and other tricks for efficiency.