Reduce code duplication by properly utilizing PartialFunction.