cleanup by splitting code plus fix some recent breakage