Refactor more logic out of client into main lib