Code for assignments 4-7