added output queue, modified flow and logic