clean up the related_to query code a bit