Fix tests and refactor permissions.