Functions

You might say that this is getting nastier and nastier. But meh, it works.
I do have vague plans for improving the code quality though