Moved all permission fixing to one method, added unit test