Update to rebuild_permissions function, should work better now