global: refactor setup and cleanup Move the common initialization and cleanup methods to reduce unnecessary duplication.