Re #7672. Refactored according to Michael's comments.