final working solution