Move flow handling to its own module