first version, needs optimisation for bigger problem