reworking ex5 based on prof's comments