working solution to prob 5, not effecient. need to circle back