Client refactoring

Hi Garreth

Consider this a work in progress, for your review. Don't merge yet, as I'm far from done.
I'll continue to commit things to this branch so this PR stays up to date.