Refactor a large pattern matching function with lenses for clarity.