logic working incorrect records returned