Move code back so memory isn't leaked on failure.

From commit 4a1d08dec99e0b7571a552936b80c83436c68eae.