Move command handling into the server and tidy up some bits.

15 files changed