core: refactoring the way sets work internally v2

This is a refactored solution for PR #616. Functionally this is still the same change, but it’s implemented a lot cleaner with less code and less changes to existing parts of TF.
Ah, check... No problem, will make it alphabetised then. I was ordering the case based on visual (short -> long (in both words and needed code in the case)) and complexity (simple structure -> complex structure) args, which are mostly the same (more complex almost always means more code).