Control flow issues refactoring