fix and improve flow functions