Some refactoring to client.py