Refactored out duplicate logic