working code till question 5.3 4c