Thanks for the review. Revised patch attached, which I think addresses all the comments.