Refactored factory method to avoid unnecessary nesting.