refactoring: extract logic to state obj