More code change (mostly removing unused code and moving other code around)