fix bugs in cut-from-flow code and add some tests for it