working solution for prob 4