Fix context manager logic