Working solution for the third question of the project