trying to reduce sql queries