Proposed fix for the activedocument management.

Second try at a decent pull request.