code restructure and stream stdin/out