Fixed search loop logic