Backport of some changes from the 1.0.5 version available on wordpress.org plugins