set internalRules last, so that it can be overrided by rules
Unified code for global and external metadata

Didn't clean up unused code though