Refactored connection factory code