Cleaned up code, and made permissions more declarative in the api.