corrected comparision algorithm