first working solution, optimization is needed