Permission system - refactor, improvement and bug fixes