made some changes in flow algorithm