Extract base functionality to client and refactor some code

A lot of changes :smiley: