wrote some of algorithm 2 in main.py