Backport some fixes from training