Add part of Lathanael's patch.