Prop. 2.3 Hartshorne: $\varphi:A\to B$ induces a morphism $\operatorname{Spec}(B)\to\operatorname{Spec}(A)$ I don't fully understand a step in the proof of the above-mentioned Proposition; more precisely, in part (b):

If $\varphi:A\to B$ is a homomorphism of rings, $X=\operatorname{Spec}(A)$, $Y=\operatorname{Spec}(B)$, then $\varphi$ induces a natural morphism of locally ringed spaces $$(f,f^*):(Y,\mathcal{O}_Y)\to(X,\mathcal{O}_X).$$

As for the proof: I understand how $f$ is constructed and why it is continuous. It's the $f^*$ part I don't quite get yet. If we localize $\varphi$ at a point $\mathfrak{p}\in Y=\operatorname{Spec}(B)$, we get a local homomorphism $\varphi_{\mathfrak{p}}:A_{\varphi^{-1}(\mathfrak{p})}\to B_{\mathfrak{p}}$. Now for an open set $U\subseteq X$ we should obtain a ring homomorphism $f^*:\mathcal{O}_X(U)\to\mathcal{O}_Y(f^{-1}(U))$.
I tried to figure out how that works: the elements of $\mathcal{O}_X(U)$ are functions $U\to\coprod_{\mathfrak{q}\in U}A_{\mathfrak{q}}$, while those of $\mathcal{O}_Y(f^{-1}(U))$ are of the form $f^{-1}(U)\to\coprod_{\mathfrak{p}\in f^{-1}(U)}B_\mathfrak{p}$. If we compose an $s\in\mathcal{O}_X(U)$ with $f$ from the left, half of what we need is there. I want to use the local homomorphisms $\varphi_\mathfrak{p}$ on the codomain of $s$, but my mind begins twisting when I try to think about the indexing set there. I'm pretty sure it's an easy 'problem'. Does maybe the first equality in
$$\coprod_{\mathfrak{q}\in U}A_\mathfrak{q}=\coprod_{\mathfrak{p}\in f^{-1}(U)}A_{\varphi^{-1}(\mathfrak{p})}=\coprod_{\varphi^{-1}(\mathfrak{p})\in U}A_{\varphi^{-1}(\mathfrak{p})}$$ already hold (so I could use the $\varphi_\mathfrak{p}$)?
Is every point in $U$ already of the form $\varphi^{-1}(\mathfrak{p})$, and is this one-to-one? I guess my thoughts here go in the wrong direction, and maybe someone is able to understand my little crisis and help me out. This construction still confuses me, in particular the switching of directions all the time.
Thank you very much in advance!
 A: In order to define $f^*:\mathcal{O}_X(U)\to\mathcal{O}_Y(f^{-1}(U))$ we are given a map $s: U\to\coprod_{\mathfrak{q}\in U}A_{\mathfrak{q}}$ with $s(\mathfrak q) \in A_\mathfrak{q}$ for all $\mathfrak q \in U$ and we want to find a map $f^*(s): f^{-1}(U) \to \coprod_{\mathfrak p \in f^{-1}(U)} B_\mathfrak p$ which takes $\mathfrak p \in f^{-1}(U)$ to some element in $B_\mathfrak p$. Well, for $\mathfrak p \in f^{-1}(U)$ we have $f(\mathfrak p) \in U$, so we can apply our $s$ to $f(\mathfrak p)$ and get $(s \circ f)(\mathfrak p) \in A_{f(\mathfrak p)}$. As you said, the ring homomorphism $\varphi: A \to B$ induces a homomorphism $\varphi_{\mathfrak p}: A_{f(\mathfrak p)} \to B_\mathfrak p$ of local rings. We can apply this to $(s \circ f)(\mathfrak p)$ and we get $(\varphi_\mathfrak p \circ s \circ f)(\mathfrak p) \in B_\mathfrak p$. Altogether, our map $f^*$ looks like this:
$$f^*: \mathcal{O}_X(U)\to\mathcal{O}_Y(f^{-1}(U))\\
(s(\mathfrak q))_{\mathfrak q \in U} \in \prod_{\mathfrak q \in U} A_\mathfrak q \mapsto ((\varphi_\mathfrak p \circ s \circ f)(\mathfrak p))_{\mathfrak p \in f^{-1}(U)} \in \prod_{\mathfrak p \in f^{-1}(U)} B_\mathfrak p$$
