Refactored the helper to remove repeated code.