Imported Tanvi's WIP patches from bug 1006881