Help explain the set being constructed in this Cantor-Schroder-Berstein proof The Cantor-Schroder-Bernstein theorem states that:

Suppose $A$ and $B$ are sets. If $|A|\le |B|$ and $|B|\le |A|$, then $|A|=|B|$

Proof:
So, $|A|\le|B|$ implies we can choose an injection $f:A\rightarrow B$ and $g:B \rightarrow A$.
Let $R=\text{Im}(g) \subseteq A$. Then, if we think of $g$ as a function from $B$ to $R$, it is a bijection and so is $g^{-1}:R \rightarrow B$.
Here's the part I don't understand:

We now define a sequence of sets $A_1,A_2,A_3,\dots$ by recursion as follows:
  $$A_1=A-R,$$
  $$\text{for every }n \in \mathbb{Z}^+, A_{n+1}=g(f(A_n))=\{g(f(a))|a\in A_n\}$$
  Let $X=\cup_{n\in\mathbb{Z}^+} A_n$ and $Y=A-X$. Of course, every element of $A$ is in either $X$ or $Y$, but not both. Now define $h:A\rightarrow B$ as follows:
  $$h(a)=\left\{ 
\begin{align}
f(a), & \text{ if } a \in X \\
g^{-1}(a), & \text{ if } a \in Y
\end{align}
\right.$$
  Note that for every $a\in A$, if $a\notin R$ then $a\in A_1 \subseteq X$. Thus, if $a\in Y$ then $a\in R$, so $g^{-1}(a)$ is defined. Therefore, this definition makes sense. 

What does the sequence of sets $A_1,A_2,A_3,\dots$ look like and what is it suppose to represent? Is the union of it all suppose to partition the set $A$, the domain of $f$?
I tried to sketch a small case but that didn't help at all as I end up having $f$ and $g$ being a surjection. 
 A: The idea of the proof is the following: if we have maps $f:A\to B$ and $g:B\to A$, we can always find $S\subseteq A$ such that $f$ maps $A\setminus S$ onto $f(A\setminus S)$ and such that $g$ maps $B\setminus f(A\setminus S)$ onto $g(B\setminus f(A\setminus S))=S$. You can draw a diagram of some sorts to see what's going on. We're partitioning $A$ into two pieces in such a way that we get surjections in each piece by means of $g$ and of $f$.  
The nontrivial task, here, is to find such set $S$ that satisfies this last equation. Note that $S$ is a fixed point of the map $\Gamma:\wp(A)\to \wp(A)$ that sends $S\to g(B\setminus f(A\setminus S))$, hence the proof of the Cantor Bernstein theorem (actually of the more general claim above), boils down to proving such fixed point exists. One can do so with the construction you've been presented which builds such set "by force". 
A cleaner proof goes as follows. The map defined above has the property that if $S'\subseteq S$, then $\Gamma(S')\subseteq \Gamma(S)$ (check this). Now consider the set of elements $\{S:S\subseteq \Gamma(S)\}$. We may take the supremum of this, i.e. the union of every set in this collection, call it $S_0$. This is the largest set that satisfies $S_0\subseteq \Gamma(S_0)$. If we apply $\Gamma$ to this inequality, we get that $\Gamma(S_0) \subseteq \Gamma\Gamma(S_0)$. This means that $\Gamma(S_0)$ is also a set in this collection. Since $S_0$ was the largest, we must have $\Gamma(S_0)\subseteq S_0$. This means, by double inclusion, that $\Gamma(S_0)=S_0$. Hence, we have found such set!
