Improve code structure and flush sysout