Several fixes and rewrites