some refactoring of flow