refactor and sync stdout