changed the algorithm a bit