refactor flow