Refactoring of flag variable and conditional