refactoring of code to reduce complications with permissions