Adapted from Chris Tralie's CS 477 at Ursinus College