Working permissions code. Cleanup needed