Added better permission checking, needs to be refactored.