[#2222] refactored to reduce copy/paste and improve clarity