Refactor, fix and add new permission tests.