Refactoring factory pattern