Segmentation fixes courtesy of Thomas Hainmüller.