Unit tests for the permissions system and a couple bug fixes