David's changes to fix permission checks and a variety of minor fixes