Refactor ClientHandler.

Mainly readability improvements. There are no functional changes in this PR as far as I could understand.

Aside from commit 331c98d, all commits only touch a single method, which makes reviewing the changes easier. That was also done to make sure there are no functional changes.