Fixed a few bugs with the permission function