If $f : X \to Z$ is a morphism of schemes, which factors through an open $i : U \to Z$ on the level of topological spaces... If $f : X \to Z$ is a morphism of schemes, which factors through an open $i : U \to Z$ on the level of topological spaces... then is there a (unique) morphism of schemes $g : X \to U$ which makes the diagram commute. 
I believe I just proved this (it is a tautology, just define the map to be the restriction of $f^{\#}$ on the relevant opens), but I want to make sure that I am not fooling myself (this stuff feels so complicated...).
Can someone comment?
 A: Let $g \colon X \to U$ denote the topological map.

Lemma 1. We have a canonical isomorphism $f_* \mathcal O_X|_U \cong g_* \mathcal O_X$.

Proof. Let $V \subseteq U$ be open. Then $f^{-1}(V) = g^{-1}(V)$, so
$$(f_* \mathcal O_X|_U)(V) = f_* \mathcal O_X (V) = \mathcal O_X (f^{-1} V) = \mathcal O_X(g^{-1}V) = (g_* \mathcal O_X)(V).$$

Lemma 2. The restriction of $f^\# \colon \mathcal O_Z \to f_* \mathcal O_X$ to $U$ gives a morphism of sheaves of rings
  $$g^\# \colon \mathcal O_U \to g_* \mathcal O_X.$$

Proof. The identification of Lemma 1 is an isomorphism of sheaves of rings.

Lemma 3. Let $z \in U$. Then $\mathcal O_{Z,z} = \mathcal O_{U, z}$.

Proof. One is the colimit over all opens $V \subseteq Z$ containing $z$, and the other over all $V \subseteq U$ containing $z$. The latter forms a cofinal system in the former, thus the colimits agree.

Proposition. The pair $(g,g^\#)$ is a morphism of locally ringed spaces.

Proof. Lemma 1 and Lemma 2 show that it is a morphism of ringed spaces. Let $x \in X$. By assumption, the map $f^\#_x \colon \mathcal O_{Z, f(x)} \to \mathcal O_{X,x}$ is a local ring homormophism, hence by Lemma 3 so is $g^\#_x \colon \mathcal O_{U,f(x)} \to \mathcal O_{X,x}$.
Remark. One then needs to go on to prove that $f$ factors through $g$, and that such a factorisation is unique. I will not carry out these steps.
