Fix for revoked permission bug. General refactoring to follow coding standards and increase performance

Subj says everything :)