Better solving algorithm