Patch by Darin and me, reviewed by Maciej.