move flow logic to lib