Separate out client and server code

Also general rearranging and tweaking