Fix context initialization and clean up