Fix by Dave MacLachlan, reviewed by Darin and Alexey.