Fix some bugs in the permissions system