Refactor control flow