Major work on d2, plus other random fixes