bsmedberg's commit with a couple of modifications

This is version 1.3, submitted to AMO just now.