Very happy with this bit of code! I assumed like most other code I've done that I'd have to troubleshoot it a lot before it worked the way I intended, but this worked very well very quickly, bar a few mistakes like using object_exists instead of instance_exists, lol.
Progress!