fix small bug in O(m^2) version