Various rewrites and fixes