Re-factor matching to group entities by pattern.
Order entities query. Also, fixes #17.
Gratuitous use of distinct for greater benefit.