code originally provided as hw3_rottenpotatoes from saasbook