Split long function
Do not duplicate info
Add some coverage of permission_check()