working solution for the problem