altered select statement to stop continouos loop