[#2212] CID43798:

Reordered alloctaions and opens to avoid needing to close and free on
early return
[#858] Removed unused variables