Thanks gbdlin for the initial patch and Tim Graham for the review.
Thanks to Claude Paroz for the original patch.