Reorder setup and cleanup functions for readability

As suggested on another patch, reordering these to have the setup
functions together for readability.

Partially-implements bp:resource-cleanup

Change-Id: I8e75d29edc62d30c2ba51f01a3bec490c88adde4
Merge "Reorder setup and cleanup functions for readability"