code from today's lecture, plus cleanups