Refactor client processing loop into it's own function