Bugs fixes and better permissions handle