updated to incorporate simplification ideas from class review.