Fixes loss of output (premature close?) and partly fixes erroneous switching of order