Refactor server to server and instance.

Extract the instance code out into an instance class and futher refactor
code into smaller methods.

As discussed with @malcyl, I've not tested this, just refactored blind,
so please give it a good testing before deploying!