refactored and made into an extension