reworking database queries