Added permission checking
Refactoring
More testing