main logic and algorithm finished.