I am stuck on understanding the solution of the (b)-part of this problem for quiet some time now. The (a)-part was very easy to solve. I still include the (a)-part, because Spivak uses it to solve the (b)-part.
(a) Suppose $g=h \circ f$. Prove that if $f(x)=f(y)$, then g(x)=g(y).
Solution: Let $f(x)=f(y)$, then $g(x)=h(f(x))=h(f(y))=g(y)$.
(b) Conversely, suppose that $f$ and $g$ are two functions such that $g(x)=g(y)$ whenever $f(x)=f(y)$. Prove that $g=h \circ f$ for some function h. Hint: Just try to define $h(z)$ when $z$ is of the form $z=f(x)$ (these are the only $z$ that matter) and use the hypothesis to show that your definition will not run into trouble.
I could not find a solution for this problem and looked up Spivak's solution which reads as follows:
(b) If $z=f(x)$, define $h(z)=g(x)$. This definition makes sense, because if $z=f(x')$, then $g(x)=g(x')$ by part (a). For $z$ not of the form $f(x)$, define $h$ any old way (or leave it undefined). Then for all $x$ in the domain of $f$ we have $g(x)=h(f(x))$.
Somehow, I find this very confusing and it seems to me that the thinking behind this proof is almost identical with the solution of the (a)-part (first suppose that $g=h \circ f$ and then let $g(x)=g(y)$ whenever $f(x)=f(y)$ to show that $g=h \circ f$ which would be absurd).
What am I missing? Why does Spivak's proof work?