More rewrite work.
Further work, mainly on getting user permissions working again.