cleaned while loops in steps 5 and 6
added legends