Refactored to use polymorphic dispatch.