moved permission checks to improve readability