almost finished with solving algorithm