Source: https://web.stonehill.edu/compsci/CS314/Assignments/Assignment2.pdf