started refactoring control flow