Updated algorithm with rectangleSearch3