query until < 500 results returned
cleaned up code