refactoring: extract method to simplify control flow