Refactor permission code