Remove flag argument
If a function takes a boolean argument, it is possible to copy the function and set the variable to true in the first function and to false in the second function. Then you can propagate the value down. This often makes it possible to simplify the code.