get rid of n-square algorithm in loop