Switch from a single function with a lot of branching at its top level to dedicated function calls with own documentation.