Fix #394. Still quite dirty, but correct at least.