hw2: all functionality (including extra credits) implemented.