tested and working ex2 for hw3