changed the algorithm a bit more