fixed issue #8, added ownsDocument into permissions, refactored code