Adding scope object

OK, this should merge fine now, after a healthy dose of rebasing.

This adds in a single scope object, so any variables/functions that need to be added to the global namespace so they can be called from the patch need to be added through this.

Seems to be working fine, one or two issues that needed fixing after the rebase, but it seems pretty solid now.