adding code from Week 4 problem set, worked with Phil