Code from Algorithms course.