Refactor client HTTP code a bit