fix fit and square algorithm