I think this is right, but seems like a bit of a hack - http://pastebin.mozilla.org/2485111