Add permission checking algorithm
Add single permission check function
Use priority counting
Trim function to support builtin permission only
Refactored code using yield generators