add USE_PERMISSIONS options and some refactoring