Working solution for the fourth question of the project