Simplify algorithm by listing and sorting instead of using a map.
Make LineCompare into a lambda.
Minor cleanups.