add option to output IO flowchart 

also reorganize code with functions