Cleanup and some error handling in the client.