Refactor the assignements to reduce complexity
Reorder the branches to extract the common patterns
Use Results to move error handling upper in the chain.
Use as much internal functions, such as map
to reduce match nesting.
Use iterators when applicable.