Metadata: metadata generation improvements

Primarily, this fixes a few potential bugs in initial metadata, which
may result in inconsistent ordering of groups, which can be a problem
when relying on category suppression.

This also deduplicates some category suppression code, and moves some
nested functions to top-level methods, which are surprisingly faster.